Waiting for data via select not working
        Posted  
        
            by BSchlinker
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by BSchlinker
        
        
        
        Published on 2010-06-07T19:41:13Z
        Indexed on 
            2010/06/07
            20:22 UTC
        
        
        Read the original article
        Hit count: 255
        
I'm currently working on a project which involves multiple clients connected to a server and waiting for data. I'm using select and monitoring the connection for incoming data. However, the client just continues to print nothing, acting as if select has discovered incoming data. Perhaps I'm attacking this wrong?
For the first piece of data the server does send, it is displayed correctly. However, the server then disconnects and the client continues to spew blank lines.
        FD_ZERO(&readnet);
        FD_SET(sockfd, &readnet);   
while(1){
            rv = select(socketdescrip, &readnet, NULL, NULL, &timeout);
            if (rv == -1) {
                perror("select"); // error occurred in select()
            } else if (rv == 0) {
                printf("Connection timeout!  No data after 10 seconds.\n");
            } else {
                // one or both of the descriptors have data
                if (FD_ISSET(sockfd, &readnet)) {
                    numbytes = recv(sockfd, buf, sizeof buf, 0);
                    printf("Data Received\n");
                    buf[numbytes] = '\0';
                    printf("client: received '%s'\n",buf);
                    sleep(10);
                }
            }
        }
© Stack Overflow or respective owner