Linux 接受时出错:资源暂时不可用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7635440/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ERROR on accept: Resource temporarily unavailable
提问by oznus
I am trying to create single threaded server in linux (red-hut) in C that will listen to multiple sockets.
我正在尝试在 C 语言的 linux (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.
否则一切正常。
回答by Jan Hudec
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.
如果您将侦听套接字设置为非阻塞,accept
则应该在没有客户端尝试连接时将 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
(或任何替代方法)等待侦听套接字或任何数据套接字上的数据。
回答by vrjuliao
maybe, set up fnctl
flags after accept()
could work.
也许,fnctl
在accept()
可以工作后设置标志。