linux - socket::accept 继续返回 EGAIN

标签 linux sockets

我使用非阻塞套接字来接收新连接。但是代码多次未能 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);
}

errnoEAGAIN

最佳答案

从联机帮助页到 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/

相关文章:

c - 使用 C 从客户端向服务器发送多个回复

sockets - SOCKET_RWGROUP=ClamAv 中的后缀?

c - GCC 中的互斥锁在哪里定义?

regex - Grep 某些字符

java - 尝试在 Jenkins 上运行 Selenium 时出现 NoSuchSession 异常

java - 客户端断开连接后,Socket isClosed() 返回 false

python - 服务器的非阻塞套接字

java - Netty 多个客户端,断开一个影响所有

Linux 系统调用 : Custom Control Messages

c - 向 pthread_create() 发送多个参数