错误的接受:资源暂时不可用
我想创建用C在linux(红色小屋)单线程服务器将侦听到多个插座。
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);
我得到:
ERROR on accept: Resource temporarily unavailable
否则,一切完美的作品。
Otherwise everything works perfectly.
资源暂时不可用是EAGAIN而这不是一个真正的错误。它的意思是我没有回答你,现在你告诉我不要等待,所以我在这里没有答案回来了。
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."
如果您设置一个监听套接字非阻塞,你似乎做,接受
应该给errno设置为该值时,有没有客户端试图连接。您可以使用选择
(传统)或调查
(语义上等效,新的界面,$ P $等待进入的连接pferred除非你需要它没有一些旧的Unix上运行)或的epoll
(千描述的优化,Linux的特定)系统调用。
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.