My tcpclient does not recognize dropped network status.

groads2

Newcomer
Joined
May 23, 2012
Messages
16
I have a service that uses the tcpClient to send and receive data. I can unplug the network cable and the service does not know the connection has been broken. This is the code that is used to test the connection. The call to send does not throw an error. How can I get my service to know the connection has broken? Any help is greatly appreciated.

Private Function IsConnected() As Boolean
SyncLock (_sendSyncObject)
If Not _tcpClient Is Nothing Then
Try
If IsConnectionInCloseWait() Then Return False
Dim data(0) as byte
_tcpClient.Client.Blocking = False
_tcpClient.Client.Send(data, 0, 0)
Return True
Catch ex As SocketException
' 10035 == WSAEWOULDBLOCK
If ex.NativeErrorCode.Equals(10035) Then Return True
Catch e As Exception
Return False
Finally
If Not _tcpClient Is Nothing AndAlso Not _tcpClient.Client Is Nothing Then _tcpClient.Client.Blocking = True
End Try
End If
End SyncLock
Return False
End Function
 
Back
Top