windows 套接字的有效性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3046359/
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
Validity of a Socket
提问by ckv
I have created a socket using the following lines of code and i get a valid socket and connection is established between the client and server machines. Is there a possibility that the socket becomes invalid due to network disturbances or any other reason.
我使用以下代码行创建了一个套接字,并获得了一个有效的套接字,并在客户端和服务器计算机之间建立了连接。是否有可能由于网络干扰或任何其他原因导致套接字失效。
If so how do we check whether the socket is valid or not.
如果是这样,我们如何检查套接字是否有效。
SOCKET SocServer;
//To Set up the sockaddr structure
ServerSock.sin_family = AF_INET;
ServerSock.sin_addr.s_addr = INADDR_ANY
ServerSock.sin_port = htons(PortNumber);//port number of 5005
// To Create a socket for listening on PortNumber
if(( SocServer = socket( AF_INET, SOCK_STREAM, 0 )) == INVALID_SOCKET )
{
return FALSE;
}
//To bind the socket with wPortNumber
if(bind(SocServer,(sockaddr*)&ServerSock,sizeof(ServerSock))!=0)
{
return FALSE;
}
// To Listen for the connection on wPortNumber
if(listen(SocServer,SOMAXCONN)!=0)
{
return FALSE;
}
I know i can check for INVALID_SOCKET which in other words means the socket is 0. Is there any other way out because my SocServer will have a value say 2500, i want to check if this socket is valid.
我知道我可以检查 INVALID_SOCKET 换句话说,这意味着套接字为 0。有没有其他出路,因为我的 SocServer 的值是 2500,我想检查这个套接字是否有效。
回答by Hasturkun
Pass your socket to any one of the windows socket functions (eg. getsockopt()
), if the socket is invalid, it will return SOCKET_ERROR
while WSAGetLastError()
will return WSAENOTSOCK
.
将您的套接字传递给任何一个 Windows 套接字函数(例如getsockopt()
),如果套接字无效,它将返回SOCKET_ERROR
而WSAGetLastError()
将返回WSAENOTSOCK
。
It is important to note that INVALID_SOCKET
does not equal 0
(the actual value, which you should not use specifically is ((SOCKET)(~0))
需要注意的是INVALID_SOCKET
不等于0
(实际值,你不应该专门使用的是((SOCKET)(~0))
回答by LukeN
The socket can become "invalid" when either side (expected or unexpected) disconnects, but not out of the blue due to network interferences (unless it disconnects, read above).
当任何一方(预期的或意外的)断开连接时,套接字可能会变得“无效”,但不会因为网络干扰而突然出现(除非它断开连接,请阅读上文)。
You can detect this by checking the return values of send()
and recv()
for -1 (SOCKET_ERROR)
您可以通过检查返回值检测这种send()
和recv()
为-1(SOCKET_ERROR)
回答by Amardeep AC9MF
The socket will not go invalid. But after you listen() you must accept() a connection. That connection may be lost at some point. It could be detected by a failure to write to the socket or recv() returning with error. Then you just recycle back to listen().
套接字不会失效。但是在你 listen() 之后你必须 accept() 一个连接。该连接可能会在某个时候丢失。可以通过写入套接字失败或 recv() 返回错误来检测到它。然后你只需回收回听()。
回答by Mark B
First, assuming that INVALID_SOCKET (which you don't show) is actually defined as 0 this is incorrect. socket
returns -1 on failure like the other system calls (0 is a valid fd that could be returned).
首先,假设 INVALID_SOCKET(您没有显示)实际上定义为 0,这是不正确的。socket
与其他系统调用一样失败时返回 -1(0 是可以返回的有效 fd)。
Then once you accept a connection, your send
/write
or recv
/read
call will return -1. If you're trying to write, errno
will be set to EPIPE
to indicate the socket was closed (by your client). There's no way to just ask if a socket is closed.
然后一旦您接受连接,您的send
/write
或recv
/read
调用将返回 -1。如果您正在尝试写入,errno
将设置EPIPE
为指示套接字已关闭(由您的客户端)。没有办法只询问套接字是否关闭。