Winsock的接受超时

问题描述:

是否有可能使用阻塞winsockets时,超时设置为接受功能?就像我们可以做的recv并通过setsockopt的发送功能?

Is it possible to set timeout to accept function when using blocking winsockets? Like we can do to recv and send function via setsockopt?

好像这是不可能的,但我要确保。

Seems like it's not possible, but I want to ensure.

选择功能可与超时使用。虽然,选择最常使用非阻塞套接字使用,我没有看到任何prevents通过阻塞套接字进行选择。如果选择功能超时,那么你的行为相当于接受超时。

Select function can be used with timeout. Though, select is most commonly used with non-blocking sockets, I haven't read anything that prevents passing a blocking socket to select. If the select function times out, then you have the behavior equivalent of accept timeout.

从MSDN

参数readfds标识是可读性进行检查插座,如果插座是目前在听的状态,它会被标记为可读,如果传入的连接请求已经收到这样的接受的是保证不阻塞,完成即可。对于其他的插座,那排队的数据可读性手段可用于阅读,这样来的recv,WSARecv时WSARecvFrom或recvfrom的通话保证不会阻止。

"The parameter readfds identifies the sockets that are to be checked for readability. If the socket is currently in the listen state, it will be marked as readable if an incoming connection request has been received such that an accept is guaranteed to complete without blocking. For other sockets, readability means that queued data is available for reading such that a call to recv, WSARecv, WSARecvFrom, or recvfrom is guaranteed not to block."

这应该给你你所期望的行为。

This should give you the behaviour you are expecting.