我使用非阻塞套接字来接收新连接。但是代码多次未能 accept()
。
int sockfd = ::socket(family, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, IPPROTO_TCP);
::bind(sockfd, bind_addr, static_cast<socklen_t>(sizeof(struct sockaddr_in6)));
ret = ::listen(sockfd, SOMAXCONN);
while (True) {
::poll(&*pollfds_.begin(), pollfds_.size(), timeoutMs);
struct sockaddr_in6 addr;
bzero(&addr, sizeof addr);
socklen_t addrlen = static_cast<socklen_t>(sizeof *addr);
int connfd = ::accept4(sockfd, sockaddr_cast(addr),
&addrlen, SOCK_NONBLOCK | SOCK_CLOEXEC);
}
errno
是 EAGAIN
。
最佳答案
从联机帮助页到 accept(2)
:
EAGAIN or EWOULDBLOCK
The socket is marked nonblocking and no connections are present to be accepted. POSIX.1-2001 allows either error to be returned for this case, and does not require these constants to have the same value, so a portable application should check for both possibilities.
这意味着在客户端连接之前调用accept
。
关于linux - socket::accept 继续返回 EGAIN,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39285440/