title: Socket client using Select
body: <p>Im trying to implement a client function which can establish connection with multiple servers using <code>select()</code> function. But I'm not a expert in using <code>select()</code> function.
  However my client can enable a connection with the multiple servers but it could not able to read messages from those multiple servers.</p>
  
  <p>As per my following example , i connected my client with two servers <code>( 192.168.100.136, 192.168.100.138)</code>.
  After connected with those two servers , my client was receiving messages from only one server (<code>192.168.100.136</code>) , instead of receiving messages from multiple servers..</p>
  
  <p>I'm not sure about the way I used <code>select()</code> function in my sample program.
  Please correct my sample application to get the desired o/p..
  Thanks in advance</p>
  
  <pre><code>int main()
  {
      int port = 10001 ;
      char ip[][32] = {"192.168.100.136","192.168.100.138"};
      int count = 2 ;
      int ret = clientSelect(ip,port,count);
      return 0 ;
  }   
  int clientSelect(char **ipAddr ,int port , int count)
      {
      SOCKET max_sd = 0;
      SOCKET socketId[10] = {0};
      SOCKET sd = 0 ;
      SOCKET client_sock[1024] = {0} ;
      fd_set readfds;
      int i ,j , ret;
      char recvBuf[1024] = "";
      char errMsg[256] = "" ;
      struct sockaddr_in server ;
  
      FD_ZERO(&amp;readfds);
      // Socket Initialization
      for(i = 0; i&lt;count ; i++)
      {
          ret = initSocket(&amp;socketId[i]);//Small function to create socket
          if (ret != 1)
          {
              return ret ;
          }
          //Server info
          server.sin_addr.s_addr  = inet_addr(ipAddr[i]);
          server.sin_family       = AF_INET;
          server.sin_port         = htons(port);
          // Conect to server
          if (connect(socketId[i], (struct sockaddr *)&amp;server , sizeof(server)) &lt; 0)
          {
              printf("connect ::Failed to connect to server %s:%d",ipAddr[i],port);
              return -1;
          }
          // Set Socket fd
          FD_SET(socketId[i], &amp;readfds); 
  
          max_sd = (max_sd&gt;socketId[i])?max_sd:socketId[i];
      }
      while(TRUE)
      {
          ret = select(max_sd + 1, &amp;readfds, NULL, NULL, NULL);
          if (ret &lt; 0)
          {
              printf("select failed\n ");
              return -1;
          }
          for ( j = 0 ; j&lt;max_sd ; j++)
          {
              sd = client_sock[j] ;
              if (FD_ISSET(sd, &amp;readfds)) 
              {
                  ret = recv(sd,(char *)recvBuf,sizeof(recvBuf), 0);
                  if(ret &gt; 0 )
                  {
                      printf("Message received from socket %d : %s\n",sd,recvBuf);
                      send(sd,(char *)recvBuf,strlen(recvBuf),0);
                  }
              }
          }
      }
  
      return ret ;
  
  }
  </code></pre>