Untitled

By Baby Mosquito, 11 Years ago, written in C++, viewed 709 times.
URL http://pb.stoleyour.com/view/5c9b3df6 Embed
Download Paste or View RawExpand paste to full width of browser
  1. #include <stdio.h>
  2. #include <netdb.h>
  3. #include <sys/types.h>
  4. #include <sys/socket.h>
  5. #include <netinet/in.h>
  6. #include <errno.h>
  7. #include <stdlib.h>
  8. #include <strings.h>
  9. #include <arpa/inet.h>
  10. #include <unistd.h>
  11. #include <string.h>
  12. #include "client.h"
  13. #include "ui_client.h"
  14.  
  15.  
  16. #define SERVER_TCP_PORT     7000    // Default port
  17. #define BUFLEN              255      // Buffer length
  18. #define USRSIZE             100
  19.  
  20. Client::Client(QWidget *parent) :
  21.     QMainWindow(parent),
  22.     ui(new Ui::Client)
  23. {
  24.     ui->setupUi(this);
  25. }
  26.  
  27. Client::~Client()
  28. {
  29.     delete ui;
  30. }
  31.  
  32. void Client::on_sendButton_clicked()
  33. {
  34.  
  35.     ui->chatwin->setText(ui->msg->text());
  36.  
  37. }
  38.  
  39. void Client::ioLoop()
  40. {
  41.     int n, bytes_to_read;
  42.     int sd, port;
  43.     struct hostent  *hp;
  44.     struct sockaddr_in server;
  45.     char  *host, *bp, rbuf[BUFLEN], sbuf[BUFLEN], **pptr;
  46.     char str[16];
  47.     bool forkFlag = true;
  48.     char usrName[USRSIZE];
  49.     char temp[BUFLEN];
  50.     char closeChat[] = "--close";
  51.     char localhost[] = "127.0.0.1";
  52.  
  53.     pid_t childpid;
  54.  
  55.     childpid = fork();
  56.  
  57.     if(childpid > 0){
  58.         /*switch(argc)
  59.         {
  60.             case 1:
  61.                 host = localhost;
  62.                 port = SERVER_TCP_PORT;
  63.             break;
  64.             case 2:
  65.                 host =  argv[1];    // Host name
  66.                 port =  SERVER_TCP_PORT;
  67.             break;
  68.             case 3:
  69.                 host =  argv[1];
  70.                 port =  atoi(argv[2]);  // User specified port
  71.             break;
  72.             default:
  73.                 fprintf(stderr, "Usage: %s host [port]\n", argv[0]);
  74.                 exit(1);
  75.         }*/
  76.  
  77.  
  78.         // Create the socket
  79.         if ((sd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
  80.         {
  81.             perror("Cannot create socket");
  82.             exit(1);
  83.         }
  84.         bzero((char *)&server, sizeof(struct sockaddr_in));
  85.         server.sin_family = AF_INET;
  86.         server.sin_port = htons(port);
  87.         if ((hp = gethostbyname(host)) == NULL)
  88.         {
  89.             fprintf(stderr, "Unknown server address\n");
  90.             exit(1);
  91.         }
  92.         bcopy(hp->h_addr, (char *)&server.sin_addr, hp->h_length);
  93.  
  94.         printf("Enter a username: ");
  95.         //fgets (usrName, sizeof(usrName), stdin);
  96.         for(int k = 0; k < USRSIZE; k++)
  97.         {
  98.             if(usrName[k] == '\n')
  99.                 usrName[k] = '\0';
  100.         }
  101.         strcat(usrName, ": ");
  102.  
  103.  
  104.         // Connecting to the server
  105.         if (connect (sd, (struct sockaddr *)&server, sizeof(server)) == -1)
  106.         {
  107.             fprintf(stderr, "Can't connect to server\n");
  108.             perror("connect");
  109.             exit(1);
  110.         }
  111.         printf("Connected:    Server Name: %s\n", hp->h_name);
  112.         pptr = hp->h_addr_list;
  113.         printf("\t\tIP Address: %s\n", inet_ntop(hp->h_addrtype, *pptr, str, sizeof(str)));
  114.         while(true){
  115.  
  116.             printf(usrName);
  117.             //gets(sbuf); // get user's text
  118.             //fgets (sbuf, BUFLEN - USRSIZE, stdin);
  119.  
  120.             if(strcmp(sbuf, closeChat) != 0)
  121.             {
  122.  
  123.                 strcpy(temp, usrName);
  124.                 strcat(temp, sbuf);
  125.                 strcpy(sbuf, temp);
  126.             }
  127.             else
  128.             {
  129.                 break;
  130.             }
  131.             // Transmit data through the socket
  132.             send (sd, sbuf, BUFLEN, 0);
  133.  
  134.             if(forkFlag)
  135.             {
  136.                 forkFlag = false;
  137.                 if(fork())
  138.                 {
  139.                     while(true)
  140.                     {
  141.                         //printf("Receive:\n");
  142.                         bp = rbuf;
  143.                         bytes_to_read = BUFLEN;
  144.  
  145.                         // client makes repeated calls to recv until no more data is expected to arrive.
  146.                         n = 0;
  147.                         while ((n = recv (sd, bp, bytes_to_read, 0)) < BUFLEN)
  148.                         {
  149.                             bp += n;
  150.                             bytes_to_read -= n;
  151.                         }
  152.                         printf ("%s\n", rbuf);
  153.                     }
  154.                 }
  155.             }
  156.  
  157.         }
  158.         fflush(stdout);
  159.         close (sd);
  160.     }
  161. }
  162.  

Reply to "Untitled"

Here you can reply to the paste above