Using select() for non-blocking sockets

The problem is that when I run them, nothing happens.

The real problem is that people have been pasting stuff from Beej for years without understanding it. That’s why I don’t really like that guide; it gives large blocks of code without really explaining them in detail.

You’re not reading anything and not sending anything; no fgets, scanf, cin, etc. Here’s what I would do:

FD_SET(sock, &read_flags);
FD_SET(STDIN_FILENO, &read_flags);

/* .. snip .. */

if(FD_ISSET(STDIN_FILENO, &read_flags)) {
    fgets(out, len, stdin);
}

This will monitor stdin and read from it when input is available; then, when the socket is writeable (FD_ISSET(sock, &write_flags)), it will send the buffer.

Leave a Comment