【发布时间】:2014-02-10 07:03:52
【问题描述】:
背景/平台:
.NET 4 / C#
我有一个通过以太网连接到嵌入式设备的 C#TcpClient。
我正在使用两个计时器 - 一个用于将接收到的数据存储到本地 Queue<MyMessage> 中,另一个用于通过 TCP 将数据从另一个 Queue<MyMessage> 发送到嵌入式设备。这两个都运行每个200 ms 并发送/读取他们必须发送/读取的任何内容。
还有另一个计时器发送一个保持活动的数据包every second(目前仅用于调试目的)。
场景/问题
建立连接后,嵌入式设备开始向我的TcpClient发送一些数据。这按预期工作(请参阅下面的消息日志)。但是,然后我关闭设备(所以它根本不起作用)。这意味着它甚至无法正确关闭 TCP。但没关系。我想测试如果在真实情况下发生这样的事情会发生什么。
但是,即使服务器(设备)不再在线,TcpClient 仍然会继续发送数据。
这里是发送代码(使用NetworkStream.Write):
while (messagesToSend.Count > 0)
{
MyMessage msg = messagesToSend.Dequeue();
clientStream.Write(msg.Data, 0, msg.Data.Length);
Debug.WriteLine(DateTime.Now.ToString() + " Sent: " + msg.MessageID);
}
发送继续持续 45-50 秒,然后最终中断。 这些是为 TcpClient 和 NetworkStream 设置的选项
client.LingerState = new LingerOption(true, 0);
client.NoDelay = true;
client.SendTimeout = 3000;
clientStream.WriteTimeout = 3000;
这是调试输出:
16:32:02 Connecting
16:32:02 Authorizing
16:32:02 Sent: 255
16:32:02 Sent: 0
16:32:02 Authorized
16:32:02 Connected
16:32:02 Received: 255
16:32:02 Received: 226
...
... some regular communication here
...
16:32:06 Received: 251
16:32:06 Sent: 0
16:32:07 Received: 251
16:32:07 Sent: 0 // At this point I have turned off the device
16:32:08 Sent: 0
16:32:09 Sent: 0
...
... every second the same message
...
16:32:54 Sent: 0
16:32:55 Sent: 0
16:32:56 Sent: 0
16:32:57 Unable to write data to the transport connection: An established connection was aborted by the software in your host machine.
16:32:57 CommunicationError
为什么连接需要很长时间才能意识到它已关闭?如果主机没有响应,NetworkStream.Write 不应该立即失败吗?
如何检测设备的电源被切断,连接不再有效?
【问题讨论】:
标签: c# connection send tcpclient