At the high level, I have created a socket, bound it to a port and flagged it to listen for incoming requests. If I then execute an "accept()" on the socket, it blocks until a connection arrives. All working as desired. If I then replace the accept() with a select(), the incoming client connection doesn't appear to cause select() to wake up.
In pseudo code, this is what I am trying:
Code: Select all
int s = socket();
bind(s, port);
listen(s);
fd_set readSet;
FD_SET(s, &readSet);
int maxFd = s;
rc = select(maxFd, &readSet, NULL, NULL, NULL);
// Don't reach here ...
Later ...
Finally found the problem and it was my coding and not any form of ESP32 issue. The parameters to select() start with "the number of file descriptors to examine" ... for example if we are watching only one file descriptor and its value is 3, then we set the readSet to 0000 1000. i.e counting from the least significant bits we say "don't check fd=0, don't check fd=1, don't check fd=2, DO check fd=3". These were set correctly thanks to the macros. However, the first parameter to select is the "number of file descriptors to check". I.e. how many "bits" of the mask should we look at. I had been mistakenly using the highest file descriptor number ... but the correct answer is "the highest file descriptor number plus 1. So the new code that works becomes:
Code: Select all
int s = socket();
bind(s, port);
listen(s);
fd_set readSet;
FD_SET(s, &readSet);
int maxFd = s;
rc = select(maxFd + 1, &readSet, NULL, NULL, NULL);
// Don't reach here ...