c - socket chat application i.e i want to chat with multiple clients at a time? -


i working on socket chat application, i.e want chat multiple clients @ time. have written folllowing program. server accepts multiple clients, able chat latest client. not able chat previous client, can explain me why?

/* tcpserver.c */  #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <errno.h> #include <string.h> #include <pthread.h>  void *thread(int *);  int main() {         int sock, connected, true = 1,n=1;      pthread_t tid;         struct sockaddr_in server_addr,client_addr;            int sin_size;          if ((sock = socket(af_inet, sock_stream, 0)) == -1) {             perror("socket");             exit(1);         }          if (setsockopt(sock,sol_socket,so_reuseaddr,&true,sizeof(int)) == -1) {             perror("setsockopt");             exit(1);         }          server_addr.sin_family = af_inet;                 server_addr.sin_port = htons(5000);             server_addr.sin_addr.s_addr = inaddr_any;         bzero(&(server_addr.sin_zero),8);          if (bind(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr))                                                                        == -1) {             perror("unable bind");             exit(1);         }          if (listen(sock, 5) == -1) {             perror("listen");             exit(1);         }      printf("\ntcpserver waiting client on port 5000");         fflush(stdout);           while(n<=5)         {               sin_size = sizeof(struct sockaddr_in);              connected = accept(sock, (struct sockaddr *)&client_addr,&sin_size);              printf("\n got connection (%s , %d)",                    inet_ntoa(client_addr.sin_addr),ntohs(client_addr.sin_port));         pthread_create(&tid,null,thread,&connected);         n++;     }        close(sock);       return 0; }   void *thread(int *nfd) {         char send_data [1024] , recv_data[1024];               int bytes_recieved;            while (1)             {               printf("\n send (q or q quit) : ");               gets(send_data);                if (strcmp(send_data , "q") == 0 || strcmp(send_data , "q") == 0)               {                 send(*nfd, send_data,strlen(send_data), 0);                 close(nfd);                 break;               }                else                  send(*nfd, send_data,strlen(send_data), 0);                 bytes_recieved = recv(*nfd,recv_data,1024,0);                recv_data[bytes_recieved] = '\0';                if (strcmp(recv_data , "q") == 0 || strcmp(recv_data , "q") == 0)               {                 close(*nfd);                 break;               }                else               printf("\n recieved data = %s " , recv_data);               fflush(stdout);             }  }        /* tcpclient.c */  #include <sys/socket.h> #include <sys/types.h> #include <netinet/in.h> #include <netdb.h> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <unistd.h> #include <errno.h>   int main()  {          int sock, bytes_recieved;          char send_data[1024],recv_data[1024];         struct hostent *host;         struct sockaddr_in server_addr;           host = gethostbyname("127.0.0.1");          if ((sock = socket(af_inet, sock_stream, 0)) == -1) {             perror("socket");             exit(1);         }          server_addr.sin_family = af_inet;             server_addr.sin_port = htons(5000);           server_addr.sin_addr = *((struct in_addr *)host->h_addr);         bzero(&(server_addr.sin_zero),8);          if (connect(sock, (struct sockaddr *)&server_addr,                     sizeof(struct sockaddr)) == -1)         {             perror("connect");             exit(1);         }          while(1)         {            bytes_recieved=recv(sock,recv_data,1024,0);           recv_data[bytes_recieved] = '\0';            if (strcmp(recv_data , "q") == 0 || strcmp(recv_data , "q") == 0)           {            close(sock);            break;           }            else            printf("\nrecieved data = %s " , recv_data);             printf("\nsend (q or q quit) : ");            gets(send_data);            if (strcmp(send_data , "q") != 0 && strcmp(send_data , "q") != 0)            send(sock,send_data,strlen(send_data), 0);            else           {            send(sock,send_data,strlen(send_data), 0);              close(sock);            break;           }          }   return 0; } 

other post debug current error. advice save time , patience if decide expand project instead of basic server - client model.

the advice is: don't use threads. poll() takes lot of resources. use select().

threads should used when need use them. john ousterhout illustrated this long time ago , reason remember when people lost debugging basic thread behaviour.

alt text


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -