title: "recv" not working even though connection was accepted
body: <p>I was checking out a program written by a stackoverflow member <strong>atanos</strong> about connecting multiple clients to a single server in 2010, I modified his program a bit to accept connection details from command line, below is the server and cient code.</p>
  
  <p><strong>Server.c</strong></p>
  
  <pre><code>int main(int argc, char **argv)
  {
    fd_set fds, readfds;
    int i, clientaddrlen, portno;
    int clientsock[2], rc, numsocks = 0, maxsocks = 2;
  
    if (argc &lt; 2){
        fprintf(stderr,"ERROR, no port provided\n");
        exit(1);
    }
  
    int serversock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  
    if (serversock == -1) perror("Socket");
  
    portno = atoi(argv[1]);
    struct sockaddr_in serveraddr, clientaddr;
    bzero(&amp;serveraddr, sizeof(struct sockaddr_in));
    serveraddr.sin_family = AF_INET;
    serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);
    serveraddr.sin_port = htons(portno);
  
    if (-1 == bind(serversock, (struct sockaddr *)&amp;serveraddr,sizeof(struct sockaddr_in))) perror("Bind");
  
    if (-1 == listen(serversock, SOMAXCONN)) perror("Listen");
  
    FD_ZERO(&amp;fds);
    FD_SET(serversock, &amp;fds);
  
    while(1) {
      readfds = fds;
      rc = select(FD_SETSIZE, &amp;readfds, NULL, NULL, NULL);
  
      if (rc == -1){
        perror("Select");
        break;
     }
  
      for (i = 0; i &lt; FD_SETSIZE; i++)
      {
        if (FD_ISSET(i, &amp;readfds)){
          if (i == serversock){
            if (numsocks &lt; maxsocks){
              clientsock[numsocks] = accept(serversock,(struct sockaddr *) &amp;clientaddr,
                  (socklen_t *)&amp;clientaddrlen);
  
              if (clientsock[numsocks] == -1) perror("Accept");
              else printf("Connection accepted\n");
  
              FD_SET(clientsock[numsocks], &amp;fds);
               numsocks++;
            }
            else
              printf("Ran out of socket space.\n");
          }
          else
          {
            int messageLength = 10;
            char message[messageLength+1];
            int numOfChRead, index = 0, limit = messageLength+1;
  
            numOfChRead = recv(clientsock[i], message, messageLength,0);
  
            if(numOfChRead &gt; 0)
                printf("Data Received !!! length: %d Message: %s", numOfChRead, message);
            else
                printf("Nothing read\n");
          }
        }
      }
    }
    close(serversock);
    return 0;
  }
  </code></pre>
  
  <p><strong>Client.c</strong></p>
  
  <pre><code>int main(int argc, char **argv)
  {
    struct sockaddr_in servaddr;
    struct hostent *server;
    int portno, bytesSent = 0;
  
    if (argc &lt; 3){
        fprintf(stderr,"Usage %s hostname port\n", argv[0]);
        exit(0);
    }
  
    portno = atoi(argv[2]);
    int sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (sock == -1) perror("Socket");
  
    server = gethostbyname(argv[1]);
  
    if (server == NULL){
        fprintf(stderr,"ERROR, no such host\n");
        exit(0);
    }
  
    bzero((void *) &amp;servaddr, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
  
    servaddr.sin_port = htons(portno);
    bcopy((char *)server-&gt;h_addr,(char *)&amp;servaddr.sin_addr.s_addr,server-&gt;h_length);
  
    if (-1 == connect(sock, (struct sockaddr *)&amp;servaddr, sizeof(servaddr))) perror("Connect");
  
    while(1) {
      char message[10];
      fgets(message, 10, stdin);
      message[10] = '\0';
  
      bytesSent = send(sock, message, strlen(message), 0);
  
      if(bytesSent == -1) printf("Sending failed with errno %d", errno);
      else printf("Sent %d characters", bytesSent);
    }
    close(sock);
  }
  </code></pre>
  
  <p>I can see that the server accepts multiple connections but when I send a message from any of the client, the recv API in server doesn't read anything. The send API at the client is successful and I can see that all the characters were sent. Can someone please comment ?</p>