我使用了异步 TCP/IP 服务器,一切正常,但是当客户端由于错误或应用程序强制退出而断开连接时,它也会由于 IO.IOException 类型的异常而关闭我的服务器。异常发生在以下子中:
Private Sub ReadCallback(ByVal result As IAsyncResult)
Try
Dim client As Client = TryCast(result.AsyncState, Client)
If client Is Nothing Then
Return
End If
'MsgBox(client.ClientID)
Dim networkStream As NetworkStream = client.NetworkStream
Dim read As Integer = networkStream.EndRead(result) **' ERRORS HERE!**
If read = 0 Then
Dim client As Client = TryCast(result.AsyncState, Client)
SyncLock Me.Clients
Me.Clients.Remove(Client.ClientID)
Return
End SyncLock
End If
当客户端与我的 TCP 服务器断开连接时,以下代码会引发 IO.IOException:
Dim read As Integer = networkStream.EndRead(result) **' ERRORS HERE!**
除了捕获 IO.IOException 之外,我该如何防止它首先创建异常。基本上我想在这种情况发生时删除我的客户端,目前作为一种解决方法,我已经删除了 IO.IOException 上的客户端并重新抛出该方法。宁愿有更好的方法。