title: How to handle multiple clients in socket programming?
body: <p>I'm new in socket programming. I'm trying to make simple client, server program where the message sent from client appears on the server window. However it works for only one client. How to handle multiple clients. Thanks in advance.
  Here's my code<br>
  Server.c  </p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;string.h&gt;
  #include &lt;arpa/inet.h&gt;
  #include &lt;errno.h&gt;
  #include &lt;unistd.h&gt;
  
  #define ERROR -1
  #define MAX_DATA 1024
  
  int main(){
    int welcomeSocket, newSocket;
    char buffer[MAX_DATA];
    int data_len;
    struct sockaddr_in serverAddr;
    int addr_size = sizeof(struct sockaddr_in);
  
    if((welcomeSocket = socket(PF_INET, SOCK_STREAM, 0)) == ERROR){
      printf("Socket: ");
      exit(-1);
    }
    /* Configure settings of the server address struct */
    serverAddr.sin_family = AF_INET;
    serverAddr.sin_port = htons(9999);
    serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
    /* Set all bits of the padding field to 0 */
    memset(serverAddr.sin_zero, '\0', sizeof serverAddr.sin_zero);  
  
    /* Bind the server address struct to the socket */
    bind(welcomeSocket, (struct sockaddr *) &amp;serverAddr, sizeof(serverAddr));
  
    /* Listen on the socket, with 5 max connection requests queued */
    if(listen(welcomeSocket,5) == 0)
      printf("Listening\n");
    else
      printf("Error\n");
  
    while(1){
      /* Accept call for the incoming connection */
      if((newSocket = accept(welcomeSocket, (struct sockaddr *)&amp;newSocket, &amp;addr_size)) == ERROR){
        perror("accept");
        exit(-1);
      }
      data_len = 1;
      while(data_len){
        /* receive msg and print */
        data_len = recv(newSocket, buffer, MAX_DATA, 0);
        if(data_len){
          buffer[data_len] = '\0';
          printf("%s\n", buffer);
        }
      }
    }
    printf("Client Disconnected\n");
    close(newSocket);
  
    return 0;
  }
  </code></pre>