Search code examples
csocketsposix-select

How to use select() on sockets properly?


I'm trying to wrap my head around calling select on sockets and I can't understand what I'm doing wrong.

setup_server_socket calls bind and listen and sets the socket to nonblocking mode.

The following code blocks on the select call it seems, not moving forward to FD_ISSET. I tried connecting a client and it seems to succeed but select never returns anything.

What's the proper way to do this?

...
int listenfd = setup_server_socket( serverPort );

if( -1 == listenfd )
    return 1;

fd_set read_fds;
FD_ZERO(&read_fds);
int fdmax = listenfd;


// loop forever
while( 1 )
{
    if (select(fdmax+1, &read_fds, NULL,NULL,NULL) == -1){
        perror("select");
        exit(4);
    }

    for (int i = 0; i<= fdmax; i++){
        printf("Testing: %d, %d\n", i, FD_ISSET(i,&read_fds));

    }return 0;
...

Solution

  • Read several times syscalls(2), select(2), select_tut(2), poll(2), errno(3) (BTW, you should prefer poll(2) to the obsolete select, which don't handle file descriptors bigger than __FD_SETSIZE, i.e. 1024 on my Linux/Debian/x86-64 system).

    Then:

    fd_set read_fds;
    FD_ZERO(&read_fds);
    int fdmax = listenfd;
    FD_SET(listenfd, &read_fds);
    

    should go inside the while(1) loop, before calling select. BTW, I recommend using poll instead of select

    Read also about the C10k problem

    Don't forget that select is changing its given fd_set-s (and usually wants them to be non-empty)...

    Perhaps use strace(1) and gdb(1). If you compile with GCC, invoke it as gcc -Wall -Wextra -g to get more warnings and debug info. If you compile your C code with Clang, consider using the Clang Static Analyzer.

    Read also Advanced Linux Programming (Wayback Machine Advanced Linux Programming). It is a free book that you could also read on paper, or download from several other places, e.g. this etc...