Originally Posted by
chris128
Nice to see that it wasnt just me that had to ignore the ObjectDisposed exception when calling EndAcceptTcpClient. Same for the IOException when calling EndRead.
I assumed there was a better way to handle this that I just couldnt figure out but seeing you do the same thing makes me feel a bit better about it
Originally Posted by
jmcilhinney
Yeah, I was hunting and hunting for some property that I could test before calling EndWhatever but I couldn't find anything, so I can only assume that catching the exceptions is the only way to go.
You two might be interested in this: VS 2010 [RESOLVED] Checking for an error before it happens [TcpListener]
I've also taken a look at the IOException that you are seeing when you call EndRead on a disposed client stream. Looks like this occurs when the internal Socket is null. You could run this check against the stream:
Code:
Imports System.Reflection
Imports System.Net.Sockets
''' <summary>
''' Provides various Tcp related functions.
''' </summary>
Public NotInheritable Class TcpUtilities
'//fields
Private Shared ReadOnly SocketPropertyInfo As PropertyInfo
'//constructors
Shared Sub New()
TcpUtilities.SocketPropertyInfo = GetType(NetworkStream).GetProperty("Socket", _
BindingFlags.NonPublic Or _
BindingFlags.Instance)
End Sub
Private Sub New()
End Sub
'//functions
''' <summary>
''' Determines whether you can safely call <see cref="NetworkStream.EndRead">EndRead</see>
''' on a <see cref="NetworkStream">NetworkStream</see> object.
''' </summary>
''' <param name="stream">
''' The <see cref="NetworkStream">NetworkStream</see>
''' to test.
''' </param>
Public Shared Function CanEndRead(ByVal stream As NetworkStream) As Boolean
If stream IsNot Nothing Then
Dim value = TcpUtilities.SocketPropertyInfo.GetValue(stream, Nothing)
Return value IsNot Nothing
End If
Return False
End Function
End Class
I realize it utilities Reflection, but it really shouldn't be that slow.