调用Socket.Receive后,C#Socket.Connected属性更改为false

时间:2021-12-06 23:57:13
int readCount;
byte[] buffer = new byte[128];
SocketError socketError;

TcpClient tcpClient = tcpListener.AcceptTcpClient();
tcpClient.Client.ReceiveTimeout = 500; // #1
// tcpClient.Client.Connected is **true** here.
readCount = tcpClient.Client.Receive(buffer, 0, buffer.Length, SocketFlags.None, out socketError); // reacCount > 0
// tcpClient.Client.Connected is **false** here.

If #1 is replaced with tcpClient.Client.Blocking = false;, tcpClient.Client.Connected has correct value(true).

如果用tcpClient.Client.Blocking = false;替换#1,则tcpClient.Client.Connected具有正确的值(true)。


I've set Socket.ReceiveTime property to 100 and invoked Socket.Receive(). Receive() returned integer value greater than zero. No exception occurred. After I do my job with copied buffer - I didn't user any of Socket related methods -, Socket.Connected property has been changed to false. Why?

我已将Socket.ReceiveTime属性设置为100并调用Socket.Receive()。 Receive()返回大于零的整数值。没有例外。在我使用复制缓冲区完成工作后 - 我没有使用任何与Socket相关的方法 - ,Socket.Connected属性已更改为false。为什么?

2 个解决方案

#1


The key might be in what TcpClient.Connected really does:

关键可能在于TcpClient.Connected真正做的事情:

The Connected property gets the connection state of the Client socket as of the last I/O operation. When it returns false, the Client socket was either never connected, or is no longer connected.

Connected属性从最后一个I / O操作开始获取Client套接字的连接状态。当它返回false时,Client套接字从未连接,或者不再连接。

Because the Connected property only reflects the state of the connection as of the most recent operation, you should attempt to send or receive a message to determine the current state. After the message send fails, this property no longer returns true. Note that this behavior is by design. You cannot reliably test the state of the connection because, in the time between the test and a send/receive, the connection could have been lost. Your code should assume the socket is connected, and gracefully handle failed transmissions.

由于Connected属性仅反映最近操作时的连接状态,因此您应尝试发送或接收消息以确定当前状态。消息发送失败后,此属性不再返回true。请注意,此行为是设计使然。您无法可靠地测试连接状态,因为在测试和发送/接收之间的时间内,连接可能已丢失。您的代码应该假定套接字已连接,并正常处理失败的传输。

So when you're not blocking and by the time you're checking the Connected value, it's possible the read wasn't finished therefore Connected is still at it's old value.

因此,当您没有阻止时,当您检查Connected值时,可能读取未完成,因此Connected仍处于旧值。

#2


This might happen if the remote host is no longer connected to the socket. Is there a timeout on the other side of the connection which might've been exceeded?

如果远程主机不再连接到套接字,则可能会发生这种情况。连接的另一端是否超时可能已被超过?

#1


The key might be in what TcpClient.Connected really does:

关键可能在于TcpClient.Connected真正做的事情:

The Connected property gets the connection state of the Client socket as of the last I/O operation. When it returns false, the Client socket was either never connected, or is no longer connected.

Connected属性从最后一个I / O操作开始获取Client套接字的连接状态。当它返回false时,Client套接字从未连接,或者不再连接。

Because the Connected property only reflects the state of the connection as of the most recent operation, you should attempt to send or receive a message to determine the current state. After the message send fails, this property no longer returns true. Note that this behavior is by design. You cannot reliably test the state of the connection because, in the time between the test and a send/receive, the connection could have been lost. Your code should assume the socket is connected, and gracefully handle failed transmissions.

由于Connected属性仅反映最近操作时的连接状态,因此您应尝试发送或接收消息以确定当前状态。消息发送失败后,此属性不再返回true。请注意,此行为是设计使然。您无法可靠地测试连接状态,因为在测试和发送/接收之间的时间内,连接可能已丢失。您的代码应该假定套接字已连接,并正常处理失败的传输。

So when you're not blocking and by the time you're checking the Connected value, it's possible the read wasn't finished therefore Connected is still at it's old value.

因此,当您没有阻止时,当您检查Connected值时,可能读取未完成,因此Connected仍处于旧值。

#2


This might happen if the remote host is no longer connected to the socket. Is there a timeout on the other side of the connection which might've been exceeded?

如果远程主机不再连接到套接字,则可能会发生这种情况。连接的另一端是否超时可能已被超过?