windows 在 Win32 中,有没有办法测试套接字是否是非阻塞的?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5489562/
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
In Win32, is there a way to test if a socket is non-blocking?
提问by Daniel Stutzbach
In Win32, is there a way to test if a socket is non-blocking?
在 Win32 中,有没有办法测试套接字是否是非阻塞的?
Under POSIX systems, I'd do something like the following:
在 POSIX 系统下,我会执行以下操作:
int is_non_blocking(int sock_fd) {
flags = fcntl(sock_fd, F_GETFL, 0);
return flags & O_NONBLOCK;
}
However, Windows sockets don't support fcntl(). The non-blocking mode is setusing ioctl with FIONBIO, but there doesn't appear to be a way to getthe current non-blocking mode using ioctl.
但是,Windows 套接字不支持 fcntl()。非阻塞模式是使用带有 FIONBIO 的 ioctl设置的,但似乎没有办法使用 ioctl获得当前的非阻塞模式。
Is there some other call on Windows that I can use to determine if the socket is currently in non-blocking mode?
Windows 上是否还有其他调用可用于确定套接字当前是否处于非阻塞模式?
采纳答案by Damon
A slightly longer answer would be: No, but you will usually know whether or not it is, because it is relatively well-defined.
稍微长一点的答案是:不,但您通常会知道它是否是,因为它的定义相对明确。
All sockets are blocking unless you explicitly ioctlsocket()
them with FIONBIO
or hand them to either WSAAsyncSelect
or WSAEventSelect
. The latter two functions "secretly" change the socket to non-blocking.
所有套接字都是阻塞的,除非您明确地ioctlsocket()
使用FIONBIO
或将它们交给WSAAsyncSelect
或WSAEventSelect
。后两个函数“秘密地”将套接字更改为非阻塞。
Since you know whether you have called one of those 3 functions, even though you cannot query the status, it is still known. The obvious exception is if that socket comes from some 3rd party library of which you don't know what exactly it has been doing to the socket.
由于您知道是否调用了这 3 个函数之一,因此即使您无法查询状态,它仍然是已知的。明显的例外是,如果该套接字来自某个 3rd 方库,您不知道它对套接字做了什么。
Sidenote: Funnily, a socket can be blocking and overlapped at the same time, which does not immediately seem intuitive, but it kind of makes sense because they come from opposite paradigms (readiness vs completion).
旁注:有趣的是,套接字可以同时阻塞和重叠,这看起来并不直观,但它有点道理,因为它们来自相反的范式(准备与完成)。
回答by EntangledLoops
Previously, you could call WSAIsBlockingto determine this. If you are managing legacy code, this may still be an option.
以前,您可以调用WSAIsBlocking来确定这一点。如果您正在管理遗留代码,这可能仍然是一个选择。
Otherwise, you could write a simple abstraction layer over the socket API. Since all sockets are blocking by default, you could maintain an internal flag and force all socket ops through your API so you always know the state.
否则,您可以在套接字 API 上编写一个简单的抽象层。由于默认情况下所有套接字都是阻塞的,因此您可以维护一个内部标志并通过您的 API 强制所有套接字操作,以便您始终知道状态。
Here is a cross-platform snippet to set/get the blocking mode, although it doesn't do exactly what you want:
这是一个用于设置/获取阻止模式的跨平台代码段,尽管它并不完全符合您的要求:
/// @author Stephen Dunn
/// @date 10/12/15
bool set_blocking_mode(const int &socket, bool is_blocking)
{
bool ret = true;
#ifdef WIN32
/// @note windows sockets are created in blocking mode by default
// currently on windows, there is no easy way to obtain the socket's current blocking mode since WSAIsBlocking was deprecated
u_long flags = is_blocking ? 0 : 1;
ret = NO_ERROR == ioctlsocket(socket, FIONBIO, &flags);
#else
const int flags = fcntl(socket, F_GETFL, 0);
if ((flags & O_NONBLOCK) && !is_blocking) { info("set_blocking_mode(): socket was already in non-blocking mode"); return ret; }
if (!(flags & O_NONBLOCK) && is_blocking) { info("set_blocking_mode(): socket was already in blocking mode"); return ret; }
ret = 0 == fcntl(socket, F_SETFL, is_blocking ? flags ^ O_NONBLOCK : flags | O_NONBLOCK);
#endif
return ret;
}
回答by Ben Hirschberg
I agree with the accepted answer, there is no official way to determine the blocking state of a socket on Windows. In case you get a socket from a third party (let's say, you are a TLS library and you get the socket from upper layer) you cannot decide if it is in blocking state or not.
我同意接受的答案,没有官方方法可以确定 Windows 上套接字的阻塞状态。如果您从第三方获得套接字(假设您是一个 TLS 库并且您从上层获得套接字),您无法确定它是否处于阻塞状态。
Despite this I have a working, unofficial and limited solution for the problem which works for me for a long time.
尽管如此,我对这个问题有一个有效的、非官方的和有限的解决方案,它对我有用很长时间。
I attempt to read 0 bytes from the socket. In case it is a blocking socket it will return 0, in case it is a non-blocking it will return -1 and GetLastError equals WSAEWOULDBLOCK.
我尝试从套接字读取 0 个字节。如果它是阻塞套接字,它将返回 0,如果它是非阻塞套接字,它将返回 -1 并且 GetLastError 等于 WSAEWOULDBLOCK。
int IsBlocking(SOCKET s)
{
int r = 0;
unsigned char b[1];
r = recv(s, b, 0, 0);
if (r == 0)
return 1;
else if (r == -1 && GetLastError() == WSAEWOULDBLOCK)
return 0;
return -1; /* In case it is a connection socket (TCP) and it is not in connected state you will get here 10060 */
}
Caveats:
注意事项:
- Works with UDP sockets
- Works with connected TCP sockets
- Doesn't work with unconnected TCP sockets
- 适用于 UDP 套接字
- 适用于连接的 TCP 套接字
- 不适用于未连接的 TCP 套接字