[Solved] Using select system call on a server socket.

User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

[Solved] Using select system call on a server socket.

Postby kolban » Wed Dec 07, 2016 5:01 pm

I seem to be having a mystery on the use of the select system call and am wondering if we know of any issues or if anyone has used it without incident?

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 ...
When I connect a client, we don't wake up ... if I replace select() with an accept(s) ... we block and wake up when a client connects. There is every possibility I have messed up and will be trying more experiments ... but I'm a big fan of sharing (and accepting) knowledge ... so if anyone has walked here before, I'll accept knowledge with gratitude.

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 ...
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

SpenZerX
Posts: 16
Joined: Sun Dec 13, 2015 9:23 am

Re: [Solved] Using select system call on a server socket.

Postby SpenZerX » Thu Dec 08, 2016 2:02 pm

I am building a project that uses select-driven socket read/writes.

My observation is:

- select works as it should be, max 16 conns
- but there are performance issues. The select should block till socket is read/write able. User code has no influence on speed. Resulting RX/TX performance in Kb/s is worse and not acceptable. Speed varies, a bumpy transmission of blocks.

May be select blocks to long or i have an RF performance problem in my lab.
Creator of Smart Connected Devices - for EcSUHA.de Project

Who is online

Users browsing this forum: Gaston1980 and 56 guests