When the client properlty disconnects, Sockets server
receives a FIN Packet. When a FIN packet is received,
NetworkStream.Read method returns 0 bytes. Simply restart
your listener every time Read method receives 0 bytes.
Hope this helps,
Aleksey Nudelman,
http://csharpcomputing.com [quoted text, click to view] >-----Original Message-----
>I have a problem with my TCP Socket Listener. I am
using a vb.net synchrounous listener to allow a client to
connect.
[quoted text, click to view] >
>I have no problem with the client connecting and
exchanging data between the two systems. The server
constantly listens, processes, and sends back response
messages. However, the other system has to disconnect on
a daily basis to do an offline backup of their system. I
don't receive any SocketExceptions when they disconnect,
but they can't reconnect to the listener once the
disconnect occurs.
[quoted text, click to view] >
>The only way their system can re-connect, is for me to
completely restart my listener.
>
>Is there a way for me to continually check the
connection state and restart the listener if it detects a
disconnect?
[quoted text, click to view] >
>I have attached the following code below. The public
sub procedure is called from a Windows Service.
>
>Public Shared Sub StartListening()
>
>Try
> ' Data buffer for incoming data.
> Dim bytes() As Byte = New [Byte]
(1024) {}
>
> 'Create IP Endpoint
> Dim IPEnd As New IPEndPoint
(IPAddress.Any, Port)
>
> ' Bind the socket to the local
endpoint and listen for incoming connections.
> listener.Bind(IPEnd)
> listener.Listen(100)
>
> ' Program is suspended while
waiting for an incoming connection.
> handler = listener.Accept
>
> Do
> ' Set the event to
nonsignaled state.
> allDone.Reset()
>
> data = Nothing
>
> ' An incoming connection
needs to be processed.
> While True
> bytes = New Byte
(1024) {}
> Dim bytesRec As
Integer = handler.Receive(bytes)
> data +=
Encoding.ASCII.GetString(bytes, 0, bytesRec)
> If data.IndexOf
(Chr(28)) > -1 Then
> Exit While
> End If
> End While
>
> ' Replace delimiter
characters
> Dim strReplace As String
= data.Replace(Chr(11), "")
> 'Replace the FS with a
space
> strReplace =
strReplace.Replace(Chr(28), "")
> 'Replace the CR with a
space
> strReplace =
strReplace.Replace(Chr(28), "")
>
> 'Process the received
message
> Dim strResponse As String
> strResponse =
ProcessMessage(strReplace)
>
> If Len(strResponse) > 0
Then
> ' Send MSA
message back to workstation
> Dim aryMsg() As
String
> aryMsg = Split
(strResponse, "|", , CompareMethod.Text)
>
> Dim strChar,
strNewStr As String
> For Each strChar
In aryMsg
> If
strChar = "MSA" Then
>
strNewStr = strNewStr & Chr(13) & strChar & "|"
> Else
>
strNewStr = strNewStr & strChar & "|"
> End If
> Next
>
> Dim strNewMSA As
String
> 'Add HL7 transmit
characters before sending to client
> strNewMSA = Chr
(11) & strNewStr & Chr(28) & Chr(13)
>
> ' Echo the data
back to the client.
> Dim msg As Byte()
= Encoding.ASCII.GetBytes(strNewMSA)
>
> 'Send return
message to requester
> handler.Send(msg)
>
> 'Set variables to
Nothing
> strNewStr =
Nothing
> strNewMSA =
Nothing
> strResponse =
Nothing
> strReplace =
Nothing
> aryMsg = Nothing
> msg = Nothing
> bytes = Nothing
> End If
> Loop
>
> 'handler.Shutdown
(SocketShutdown.Both)
> 'handler.Close()
>
> 'listener.Shutdown
(SocketShutdown.Both)
> 'listener.Close()
>
> Catch ex As Exception
> Dim clsError As FileUtilities
> clsError = New FileUtilities
> With clsError
> .CreateErrorLog
(0, "Source: " & ex.Source & vbCrLf & "Message: " &
ex.Message)
[quoted text, click to view] > End With
>
> Catch ex As SocketException
> Dim clsError As FileUtilities
> clsError = New FileUtilities
> With clsError
> .CreateErrorLog
(ex.ErrorCode, "Source: " & ex.Source & vbCrLf
& "Message: " & ex.Message)
[quoted text, click to view] > End With
> End Try
>
> Exit Sub
>
> End Sub 'StartListening
>
>
>--------------------------------
>From: Jimmy Chapman
>
>-----------------------
>Posted by a user from .NET 247
(
http://www.dotnet247.com/)
>
><Id>B49U81iiUUuurOcpILQT7A==</Id>
>.