我有一个类可以处理TcpClients。什么类应该做的是:如何在.NET中关闭另一端时关闭TcpClient?
while the other end has not done a graceful close or we are stopping
{
receive a request
process it
send response
}
由于我不知道什么时候其他客户端会发送一个请求,我不能做一个阅读的超时设置,所以我有什么到现在为止是这样的:
While Not Me.Stopping()
Try
If tcpClient.Available >= My.Settings.minimumModBusTcpFrameSize Then
processer = New MessageProcesser(Me, tcpClient)
processer.ProcessMessage()
End If
Catch ex As TimeoutException
''#Do not nothing, the current message will timeout on origin too.
End Try
End While
这种方法的问题是,我永远不知道客户端何时完成对Close()的远程调用。
有没有办法解决这个问题?
我发现与读取和IOException异常,而不是TimeoutException异常的问题。我只是在调查,并会返回结果。 – 2009-12-29 11:19:54
看起来像当我有一个超时TcpClient断开连接,并关闭套接字,所以我不能再次重试读取。 – 2009-12-29 11:51:31