I am trying to create single threaded server in linux (red-hut) in C that will listen to multiple sockets.
我试图在C中创建单线程服务器(red-hut),它将监听多个套接字。
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.
否则一切都很完美。
1 个解决方案
#1
13
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."
资源暂时不可用是EAGAIN,这不是一个真正的错误。这意味着“我现在没有给你答案,你告诉我不要等,所以我在这里回答没有答案。”
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.
如果您将侦听套接字设置为非阻塞,则在没有客户端尝试连接时,应该将errno设置为该值。您可以使用select(传统)或poll(语义等效,更新的接口,首选,除非您需要在没有它的情况下在某些旧的unix上运行)或epoll(针对数千个描述符,特定于Linux的)系统调用进行优化等待传入连接。
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.
当然,您将使用poll(或任何替代方法)等待侦听套接字或任何数据套接字上的数据。
#1
13
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."
资源暂时不可用是EAGAIN,这不是一个真正的错误。这意味着“我现在没有给你答案,你告诉我不要等,所以我在这里回答没有答案。”
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.
如果您将侦听套接字设置为非阻塞,则在没有客户端尝试连接时,应该将errno设置为该值。您可以使用select(传统)或poll(语义等效,更新的接口,首选,除非您需要在没有它的情况下在某些旧的unix上运行)或epoll(针对数千个描述符,特定于Linux的)系统调用进行优化等待传入连接。
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.
当然,您将使用poll(或任何替代方法)等待侦听套接字或任何数据套接字上的数据。