0

Here is my example code:

#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <errno.h>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>

int main()
{
    int fd = socket(AF_INET, SOCK_STREAM, 0);
    printf("--> fd is %d, FD_SETSIZE is %d\n", fd, FD_SETSIZE);
    fd_set set;
    FD_ZERO(&set);
    FD_SET(fd, &set);
    int retval = select(fd + 1, &set, NULL, NULL, NULL);
    if(-1 == retval)
    {
        perror("select");
    }
    printf("retval is %d\n", retval);
}

Why does select not fail or block/timeout? (retval is 1, and fd is set in set) But on an an openend socket, there is no data availible. Is there anything in the documentation i missed?

kuga
  • 1,483
  • 1
  • 17
  • 38
  • 3
    Possibly related to the problem, but I doubt it: You pass the wrong value for the first argument of `select`. It should be the highest descriptor plus one. In your case `fd + 1`. – Some programmer dude Nov 03 '17 at 17:06
  • Same mistakes as in [your last question](https://stackoverflow.com/q/47098097/694576): Wrong include, wrong nfds! – alk Nov 03 '17 at 17:06
  • More related to your problem, what *is* `select` returning? What is the output of the program you show? What output did you expect? – Some programmer dude Nov 03 '17 at 17:08
  • 3
    If `select` indicates that a file descriptor is "ready", that does not necessarily mean there is data to read or space to write. It just means that the read or write operation won't block. – Ian Abbott Nov 03 '17 at 17:21
  • @alk Again, this does not matter. – kuga Nov 03 '17 at 17:22
  • @someprogrammerdude I added your suggestions to the question – kuga Nov 03 '17 at 17:23
  • @IanAbbott This might be the clue here. A following `read` actually does not block but fail. – kuga Nov 03 '17 at 17:24
  • ... although my "won't block" is the wrong phrase, since the descriptor might be non-blocking. What it really means is that the descriptor has something to report. For the "read" set, the thing to report could be "data available" or "read will error". For the "write" set, the thing to report could be "space available" or "write will error". – Ian Abbott Nov 03 '17 at 17:41
  • I just did the same thing on winsock2. There it blocks :( – kuga Nov 06 '17 at 12:41

0 Answers0