title: Single TCP server and multiple clients - server seems to be communicating only with last conected clients
body: <p>I am using select() function and trying to run multiple clients. Each client is independent process. But the server seems to be interested only in the last connected clients.</p>
  
  <p>These is my client.c </p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;string.h&gt;
  #include "mpi.h"
  #define PORT 8888
  
  int main(int argc, char const *argv[])
  {
      /* Starting the MPI verion from here */
  
      // Initialize the MPI environment
      MPI_Init(NULL, NULL);
  
      // Get the number of generators to be run in parallel
      int number_of_clients;
      MPI_Comm_size(MPI_COMM_WORLD, &amp;number_of_clients);
  
      // Get the rank of each generator
      int client_rank;
      MPI_Comm_rank(MPI_COMM_WORLD, &amp;client_rank);
  
      struct sockaddr_in address;
      int sock = 0, valread;
      struct sockaddr_in serv_addr;
      char *message1 = "Hello from client";
      char *message2 = "I am disconnecting";
      char buffer[1024] = {0};
      if ((sock = socket(AF_INET, SOCK_STREAM, 0)) &lt; 0)
      {
          printf("\n Socket creation error \n");
          return -1;
      }
  
      memset(&amp;serv_addr, '0', sizeof(serv_addr));
  
      serv_addr.sin_family = AF_INET;
      serv_addr.sin_port = htons(PORT);
  
      // Convert IPv4 and IPv6 addresses from text to binary form
      if(inet_pton(AF_INET, "127.0.0.1", &amp;serv_addr.sin_addr)&lt;=0) 
      {
          printf("\nInvalid address/ Address not supported \n");
          return -1;
      }
  
      if (connect(sock, (struct sockaddr *)&amp;serv_addr, sizeof(serv_addr)) &lt; 0)
      {
          printf("\nConnection Failed \n");
          return -1;
      }
      send(sock , message1 , strlen(message1) , 0 );
      printf("Hello message sent from client_rank %d\n",client_rank);
      valread = read( sock , buffer, 1024);
  
  
      buffer[valread] = '\0';
      printf("The message received from server by client_rank %d is %s\n", client_rank, buffer );
      send(sock, message2, strlen(message2), 0);
      printf("The message sent again from client_rank %d is %s\n", client_rank, message2);
      close(sock);
      // Finalize the MPI environment.
      MPI_Finalize();
      /*Ending the parallel version here*/
  
      return 0;
  }
  </code></pre>
  
  <p>This is my server.c</p>
  
  <pre><code>#include &lt;unistd.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;stdlib.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;   //close 
  #include &lt;sys/types.h&gt; 
  #include &lt;sys/time.h&gt; //FD_SET, FD_ISSET, FD_ZERO macros 
  
  #define PORT 8888
  
  int main(int argc, char const *argv[])
  {
      int totalconnections = 0;
      int server_fd, new_socket, valread;
      struct sockaddr_in address, cli_addr;;
      int opt = 1;
      int addrlen = sizeof(address);
      char buffer[1024] = {0};
      char *hello = "Hello from server";
  
      socklen_t clilen;
      int closed_connections = 0;
      int client_socket[3] , max_clients = 2 , activity, i , sd, max_sd;
      int connected_clients = 0;  
      fd_set readfds;
  
      //initialise all client_socket[] to 0 so not checked 
      for (i = 0; i &lt; max_clients; i++)  
      {  
          client_socket[i] = 0;  
      } 
  
      // Creating socket file descriptor
      if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0)
      {
          perror("socket failed");
          exit(EXIT_FAILURE);
      }
  
      // Forcefully attaching socket to the port 8080
      if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR,(char *)&amp;opt, sizeof(opt)) &lt; 0)
      {
          perror("setsockopt");
          exit(EXIT_FAILURE);
      }
      address.sin_family = AF_INET;
      address.sin_addr.s_addr = INADDR_ANY;
      address.sin_port = htons( PORT );
  
      // Bind the server to the port and address
      if (bind(server_fd, (struct sockaddr *)&amp;address,sizeof(address))&lt;0)
      {
          perror("Binding Error : ");
          exit(EXIT_FAILURE);
      }
      //listen to the connections
      if (listen(server_fd, 5) &lt; 0)
      {
          perror("Listen Error : ");
          exit(EXIT_FAILURE);
      }
  
      while(1)  
      {  
          //clear the socket set 
          FD_ZERO(&amp;readfds);  
  
          //add master socket to set 
          FD_SET(server_fd, &amp;readfds);  
          max_sd = server_fd;  
  
          //add child sockets to set 
          for ( i = 0 ; i &lt; max_clients ; i++)  
          {  
              //socket descriptor 
              sd = client_socket[i];                 
              //if valid socket descriptor then add to read list 
              if(sd &gt; 0)  
                  FD_SET( sd , &amp;readfds);                  
              //highest file descriptor number, need it for the select function 
              if(sd &gt; max_sd)  
                  max_sd = sd;  
          }      
          //wait for an activity on one of the sockets , timeout is NULL, so wait indefinitely 
          activity = select( max_sd + 1 , &amp;readfds , NULL , NULL , NULL); 
  
          if ((activity &lt; 0) &amp;&amp; (errno!=EINTR))  printf("select error"); 
  
          //If something happened on the master socket ,then its an incoming connection 
          if (FD_ISSET(server_fd, &amp;readfds))  
          {  
              if ((new_socket = accept(server_fd, (struct sockaddr *)&amp;address,(socklen_t*)&amp;addrlen))&lt;0)  
              {  
                  perror("accept");  
                  exit(EXIT_FAILURE);  
              }  
  
              //Print out the details of the new connection 
              printf("New connection , socket fd is %d , ip is : %s , port : %d\n" , new_socket , inet_ntoa(address.sin_addr) , ntohs(address.sin_port));   
  
              //add new socket to array of sockets 
              for (i = 0; i &lt; max_clients; i++)  
              {  
                  //if position is empty 
                  if( client_socket[i] == 0 )  
                  {  
                      client_socket[i] = new_socket;                         
                      break;  
                  }  
              }   
          }  
  
          //else its some IO operation on some other socket
          else
          {
              for (i = 0; i &lt; max_clients; i++)  
              {  
                  sd = client_socket[i];  
  
                  if (FD_ISSET( sd , &amp;readfds))  
                  {  
                      //Read the incoming message. If valread = 0, then that client disconnected
                      valread = read( sd , buffer, 1024) ;
                      if(valread == 0) 
                      {  
                          //Somebody disconnected , get his details and print 
                          getpeername(sd , (struct sockaddr*)&amp;address ,(socklen_t*)&amp;addrlen);  
                          printf("Host disconnected , ip %s , port %d \n" ,inet_ntoa(address.sin_addr) , ntohs(address.sin_port));  
  
                          //Increase the closed connection by 1    
                          closed_connections++;  
                          client_socket[i] = 0;  
                      }  
  
                      //Send message to the client that sent the message
                      else
                      {  
  
                          buffer[valread] = '\0'; 
                          getpeername(sd , (struct sockaddr*)&amp;address ,(socklen_t*)&amp;addrlen);
                          printf("Message received from client connected to port %d is %s \n",ntohs(address.sin_port), buffer );
                          send(sd , hello , strlen(hello) , 0 );
                          printf("Hello message sent to client connected on port %d\n", ntohs(address.sin_port));
                      }  
                  }  
              }            
          }
  
          //If all the clients disconnected then break out of while loop
          if(closed_connections == max_clients) break;
      }
      close(server_fd);
      return 0;
  }
  </code></pre>
  
  <p><strong>Compiling client.c</strong> </p>
  
  <pre><code>gcc client.c -o client
  </code></pre>
  
  <p><strong>Compiling server.c</strong></p>
  
  <pre><code>mpicc server.c -o server
  </code></pre>
  
  <p>When I run the server and client using the command <strong>./client</strong> and <strong>mpirun -np 2 ./client</strong>, I get the following output on the <strong>server side</strong></p>
  
  <pre><code> New connection , socket fd is 4 , ip is : 127.0.0.1 , port : 57448
      New connection , socket fd is 5 , ip is : 127.0.0.1 , port : 57450
      Message received from client connected to port 57450 is Hello from client 
      Hello message sent to client connected on port 57450
      Message received from client connected to port 57450 is Hello from client 
      Hello message sent to client connected on port 57450
      Host disconnected , ip 127.0.0.1 , port 57448 
      Host disconnected , ip 127.0.0.1 , port 57450 
  </code></pre>
  
  <p>The output from <strong>client side</strong> is :</p>
  
  <pre><code>Hello message sent from client_rank 0
  Hello message sent from client_rank 1
  The message received from server by client_rank 0 is Hello from server
  The message received from server by client_rank 1 is Hello from server
  </code></pre>
  
  <p>I do not know, why the server does not care about the old client. </p>
  
  <p>The second problem that I also noticed is that the server does not receive the message2 sent from the client even though I have used select() function. I think that since I have used select function , server should track any send or disconnect request sent from the client.</p>
  
  <p>Any kind of help would be appreciated.</p>