Socket select() works in Windows and times out in Linux

c++, linux, select, sockets

Solution

I think the first parameter to `select()` should be `socket+1`.

You really should use another name as `socket` also is used for other things. Usually `sock` is used.

Problem

I'm porting a windows network application to linux and faced a timeout problem with select call on linux. The following function blocks for the entire timeout value and returns while I checked with a packet sniffer that client has already sent the data. ``` int recvTimeOutTCP( SOCKET socket, long sec, long usec ) { struct timeval timeout; fd_set fds;. timeout.tv_sec = sec; timeout.tv_usec = usec; FD_ZERO( &fds ); FD_SET( socket, &fds ); // Possible return values: // -1: error occurred // 0: timed out // > 0: data ready to be read cerr << "Waiting on fd " << socket << endl; return select(1, &fds, 0, 0, &timeout); } ```

Original source