这个问题与(非常接近)在非阻塞套接字连接中,select()始终返回1;但是,我似乎找不到我的代码步履蹒跚的地方。
我正在使用非阻塞套接字,并且想在将客户端连接到服务器以检查超时/成功时使用select()。问题是select()几乎总是立即返回1,即使我什至没有服务器在运行,也没有什么可连接的。预先感谢您的帮助,代码片段如下:
//Loop through the addrinfo structs and try to connect to the first one we can
for(p = serverinfo; p != NULL; p = p->ai_next) {
if ((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1)
{
//We couldn't create the socket, try again
perror("client: socket");
continue;
}
//Set the socket to non-blocking
int flags = fcntl(sockfd, F_GETFL, 0);
fcntl(sockfd, F_SETFL, flags | O_NONBLOCK);
if (connect(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
//The error was something other than non-block/in progress, …Run Code Online (Sandbox Code Playgroud)