如何关闭TcpClient的时候,另一端已经在.NET关闭? [英] How to close a TcpClient when the other end has been closed in .NET?
问题描述
我有一个处理TcpClients类。什么类应该做的是:
I have a class that handles TcpClients. What the class should do is:
while the other end has not done a graceful close or we are stopping
{
receive a request
process it
send response
}
由于我不知道什么时候其他的客户端会发送一个请求,我不能做一个读一个超时设置,所以我有什么到现在为止是这样的:
As I don't know when the other client will send a request I can not do a Read with a timeout set, so what I have until now is this:
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()。
The problem with this approach is that I never know when a client has done a remote call to Close().
有没有解决这个问题的方法是什么?
Is there a way of solving this problem?
推荐答案
解决的办法是,在这个问题:
The solution is at this question:
<一个href="http://stackoverflow.com/questions/1975040/how-to-read-with-a-tcpclient-until-the-other-side-closes-or-we-stop-in-net">http://stackoverflow.com/questions/1975040/how-to-read-with-a-tcpclient-until-the-other-side-closes-or-we-stop-in-net
这篇关于如何关闭TcpClient的时候,另一端已经在.NET关闭?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!