I am trying to create single threaded server in linux (red-hut) in C that will listen to multiple sockets.
I need to use non-blocking sockets, when I set the flags to non-blocking like this:
int flagss = fcntl(socketfds[j],F_GETFL,0);
flagss |= O_NONBLOCK;
fcntl(socketfds[j],F_SETFL,flagss);
I get:
ERROR on accept: Resource temporarily unavailable
Otherwise everything works perfectly.
Resource temporarily unavailable is EAGAIN and that's not really an error. It means "I don't have answer for you right now and you have told me not to wait, so here I am returning without answer."
If you set a listening socket to non-blocking as you seem to do, accept
is supposed to set errno to that value when there are no clients trying to connect. You can wait for incoming connection using the select
(traditional) or poll
(semantically equivalent, newer interface, preferred unless you need to run on some old unix without it) or epoll
(optimized for thousands of descriptors, Linux-specific) system calls.
Of course you will be using poll
(or any of the alternatives) to wait for data on the listening socket or any of the data sockets.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With