title: How to know if the client has terminated in sockets
body: <p>Suppose, I have a connected socket after writing this code..</p>
  
  <pre><code>if ((sd = accept(socket_d, (struct sockaddr *)&amp;client_addr, &amp;alen)) &lt; 0)
  {
      perror("accept failed\n");
      exit(1);
  }
  </code></pre>
  
  <p><strong>How can I know at the server side that client has exited.</strong></p>
  
  <p>My whole program actually does the following..</p>
  
  <ul>
  <li>Accepts a connection from client</li>
  <li>Starts a new thread that reads messages from that particular client and then broadcast this message to all the connected clients.</li>
  </ul>
  
  <p>If you want to see the whole code... In this whole code. I am also struggling with one more problem that whenever I kill a client with Ctrl+C, my server terminates abruptly.. It would be nice <strong>if anyone could suggest what the problem is..</strong></p>
  
  <pre><code>#include &lt;sys/types.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;arpa/inet.h&gt;
  #include &lt;netdb.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;signal.h&gt;
  #include &lt;errno.h&gt;
  #include &lt;pthread.h&gt;
  
  /*CONSTANTS*/
  #define DEFAULT_PORT 10000 
  #define LISTEN_QUEUE_LIMIT 6
  #define TOTAL_CLIENTS 10
  #define CHAR_BUFFER 256
  
  /*GLOBAL VARIABLE*/
  int current_client = 0;
  int connected_clients[TOTAL_CLIENTS];
  extern int errno;
  
  void *client_handler(void * socket_d);
  
  int main(int argc, char *argv[])
  {
      struct sockaddr_in server_addr;/* structure to hold server's address*/
      int    socket_d;             /* listening socket descriptor       */
      int    port;           /* protocol port number              */
      int    option_value;   /* needed for setsockopt             */
      pthread_t tid[TOTAL_CLIENTS];
      port = (argc &gt; 1)?atoi(argv[1]):DEFAULT_PORT;
  
      /* Socket Server address structure */
      memset((char *)&amp;server_addr, 0, sizeof(server_addr)); 
      server_addr.sin_family = AF_INET;               /* set family to Internet */
      server_addr.sin_addr.s_addr = INADDR_ANY;       /* set the local IP address */
      server_addr.sin_port = htons((u_short)port);    /* Set port */
  
      /* Create socket */
      if ( (socket_d = socket(PF_INET, SOCK_STREAM, 0)) &lt; 0) {
          fprintf(stderr, "socket creation failed\n");
          exit(1);
      }
  
      /* Make listening socket's port reusable */
      if (setsockopt(socket_d, SOL_SOCKET, SO_REUSEADDR, (char *)&amp;option_value, 
                  sizeof(option_value)) &lt; 0) {
          fprintf(stderr, "setsockopt failure\n");
          exit(1);
      }
  
      /* Bind a local address to the socket */
      if (bind(socket_d, (struct sockaddr *)&amp;server_addr, sizeof(server_addr)) &lt; 0) {
          fprintf(stderr, "bind failed\n");
          exit(1);
      }
  
      /* Specify size of request queue */
      if (listen(socket_d, LISTEN_QUEUE_LIMIT) &lt; 0) {
          fprintf(stderr, "listen failed\n");
          exit(1);
      }
  
      memset(connected_clients,0,sizeof(int)*TOTAL_CLIENTS);
  
      for (;;)
      {
          struct sockaddr_in client_addr;    /* structure to hold client's address*/
          int    alen = sizeof(client_addr); /* length of address                 */
          int    sd;                /* connected socket descriptor */
  
          if ((sd = accept(socket_d, (struct sockaddr *)&amp;client_addr, &amp;alen)) &lt; 0)
          {
              perror("accept failed\n");
              exit(1);
          }
          else printf("\n I got a connection from (%s , %d)\n",inet_ntoa(client_addr.sin_addr),ntohs(client_addr.sin_port));
  
          if (pthread_create(&amp;tid[current_client],NULL,(void *)client_handler,(void *)sd) != 0)
          {
              perror("pthread_create error");
              continue;
          }
          connected_clients[current_client]=sd;
          current_client++; /*Incrementing Client number*/
      }
  
      return 0;
  }
  
  void *client_handler(void *connected_socket)
  {
      int sd;
      sd = (int)connected_socket;
      for ( ; ; ) 
      {
          ssize_t n;
          char buffer[CHAR_BUFFER];
          for ( ; ; )
          {
              if (n = read(sd, buffer, sizeof(char)*CHAR_BUFFER) == -1)
              {
                  perror("Error reading from client");
                  pthread_exit(1);
              }
              int i=0;
              for (i=0;i&lt;current_client;i++)
              {
                  if (write(connected_clients[i],buffer,sizeof(char)*CHAR_BUFFER) == -1)
                      perror("Error sending messages to a client while multicasting");
              }
          }
      }
  }
  </code></pre>
  
  <p>My client side is this (Maye be irrelevant while answering my question)</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;netdb.h&gt; 
  #include &lt;string.h&gt;
  #include &lt;stdlib.h&gt;
  
  void error(char *msg)
  {
      perror(msg);
      exit(0);
  }
  
  void *listen_for_message(void * fd)
  {
      int sockfd = (int)fd;
      int n;
      char buffer[256];
      bzero(buffer,256);
      printf("YOUR MESSAGE: ");
      fflush(stdout);
      while (1)
      {
          n = read(sockfd,buffer,256);
          if (n &lt; 0) 
              error("ERROR reading from socket");
          if (n == 0) pthread_exit(1);
          printf("\nMESSAGE BROADCAST: %sYOUR MESSAGE: ",buffer);
          fflush(stdout);
      }
  }
  
  int main(int argc, char *argv[])
  {
      int sockfd, portno, n;
      struct sockaddr_in serv_addr;
      struct hostent *server;
      pthread_t read_message;
      char buffer[256];
      if (argc &lt; 3) {
          fprintf(stderr,"usage %s hostname port\n", argv[0]);
          exit(0);
      }
      portno = atoi(argv[2]);
      sockfd = socket(AF_INET, SOCK_STREAM, 0);
      if (sockfd &lt; 0) 
          error("ERROR opening socket");
      server = gethostbyname(argv[1]);
      if (server == NULL) {
          fprintf(stderr,"ERROR, no such host\n");
          exit(0);
      }
      bzero((char *) &amp;serv_addr, sizeof(serv_addr));
      serv_addr.sin_family = AF_INET;
      bcopy((char *)server-&gt;h_addr, 
              (char *)&amp;serv_addr.sin_addr.s_addr,
              server-&gt;h_length);
      serv_addr.sin_port = htons(portno);
      if (connect(sockfd,&amp;serv_addr,sizeof(serv_addr)) &lt; 0) 
          error("ERROR connecting");
      bzero(buffer,256);
      if (pthread_create(&amp;read_message,NULL,(void *)listen_for_message,(void *)sockfd) !=0 )
      {
          perror("error creating thread");
      }
      while (1)
      {
          fgets(buffer,255,stdin);
          n = write(sockfd,buffer,256);
          if (n &lt; 0) 
              error("ERROR writing to socket");
          bzero(buffer,256);
      }
      return 0;
  }
  </code></pre>