all groups > dotnet sdk > august 2004 >
You're in the

dotnet sdk

group:

Socket Listener Issue



Socket Listener Issue Jimmy Chapman via .NET 247
8/9/2004 10:34:36 AM
dotnet sdk: I have a problem with my TCP Socket Listener=2E I am using a=
vb=2Enet synchrounous listener to allow a client to connect=2E

I have no problem with the client connecting and exchanging data=
between the two systems=2E The server constantly listens,=
processes, and sends back response messages=2E However, the other=
system has to disconnect on a daily basis to do an offline=
backup of their system=2E I don't receive any SocketExceptions=
when they disconnect, but they can't reconnect to the listener=
once the disconnect occurs=2E

The only way their system can re-connect, is for me to completely=
restart my listener=2E

Is there a way for me to continually check the connection state=
and restart the listener if it detects a disconnect?

I have attached the following code below=2E The public sub=
procedure is called from a Windows Service=2E

Public Shared Sub StartListening()

Try
=09=09=09' Data buffer for incoming data=2E
=09=09=09Dim bytes() As Byte =3D New [Byte](1024) {}

=09=09=09'Create IP Endpoint
=09=09=09Dim IPEnd As New IPEndPoint(IPAddress=2EAny, Port)

=09=09=09' Bind the socket to the local endpoint and listen for=
incoming connections=2E
=09=09=09listener=2EBind(IPEnd)
=09=09=09listener=2EListen(100)

=09=09=09' Program is suspended while waiting for an incoming=
connection=2E
=09=09=09handler =3D listener=2EAccept

=09=09=09Do
=09=09=09=09' Set the event to nonsignaled state=2E
=09=09=09=09allDone=2EReset()

=09=09=09=09data =3D Nothing

=09=09=09=09' An incoming connection needs to be processed=2E
=09=09=09=09While True
=09=09=09=09=09bytes =3D New Byte(1024) {}
=09=09=09=09=09Dim bytesRec As Integer =3D handler=2EReceive(bytes)
=09=09=09=09=09data +=3D Encoding=2EASCII=2EGetString(bytes, 0, bytesRec)
=09=09=09=09=09If data=2EIndexOf(Chr(28)) > -1 Then
=09=09=09=09=09=09Exit While
=09=09=09=09=09End If
=09=09=09=09End While

=09=09=09=09' Replace delimiter characters
=09=09=09=09Dim strReplace As String =3D data=2EReplace(Chr(11), "")
=09=09=09=09'Replace the FS with a space
=09=09=09=09strReplace =3D strReplace=2EReplace(Chr(28), "")
=09=09=09=09'Replace the CR with a space
=09=09=09=09strReplace =3D strReplace=2EReplace(Chr(28), "")

=09=09=09=09'Process the received message
=09=09=09=09Dim strResponse As String
=09=09=09=09strResponse =3D ProcessMessage(strReplace)

=09=09=09=09If Len(strResponse) > 0 Then
=09=09=09=09=09' Send MSA message back to workstation
=09=09=09=09=09Dim aryMsg() As String
=09=09=09=09=09aryMsg =3D Split(strResponse, "|", , CompareMethod=2EText)

=09=09=09=09=09Dim strChar, strNewStr As String
=09=09=09=09=09For Each strChar In aryMsg
=09=09=09=09=09=09If strChar =3D "MSA" Then
=09=09=09=09=09=09=09strNewStr =3D strNewStr & Chr(13) & strChar & "|"
=09=09=09=09=09=09Else
=09=09=09=09=09=09=09strNewStr =3D strNewStr & strChar & "|"
=09=09=09=09=09=09End If
=09=09=09=09=09Next

=09=09=09=09=09Dim strNewMSA As String
=09=09=09=09=09'Add HL7 transmit characters before sending to client
=09=09=09=09=09strNewMSA =3D Chr(11) & strNewStr & Chr(28) & Chr(13)

=09=09=09=09=09' Echo the data back to the client=2E
=09=09=09=09=09Dim msg As Byte() =3D Encoding=2EASCII=2EGetBytes(strNewMSA)

=09=09=09=09=09'Send return message to requester
=09=09=09=09=09handler=2ESend(msg)

=09=09=09=09=09'Set variables to Nothing
=09=09=09=09=09strNewStr =3D Nothing
=09=09=09=09=09strNewMSA =3D Nothing
=09=09=09=09=09strResponse =3D Nothing
=09=09=09=09=09strReplace =3D Nothing
=09=09=09=09=09aryMsg =3D Nothing
=09=09=09=09=09msg =3D Nothing
=09=09=09=09=09bytes =3D Nothing
=09=09=09=09End If
=09=09=09Loop

=09=09=09'handler=2EShutdown(SocketShutdown=2EBoth)
=09=09=09'handler=2EClose()

=09=09=09'listener=2EShutdown(SocketShutdown=2EBoth)
=09=09=09'listener=2EClose()

=09=09Catch ex As Exception
=09=09=09Dim clsError As FileUtilities
=09=09=09clsError =3D New FileUtilities
=09=09=09With clsError
=09=09=09=09=2ECreateErrorLog(0, "Source: " & ex=2ESource & vbCrLf &=
"Message: " & ex=2EMessage)
=09=09=09End With

=09=09Catch ex As SocketException
=09=09=09Dim clsError As FileUtilities
=09=09=09clsError =3D New FileUtilities
=09=09=09With clsError
=09=09=09=09=2ECreateErrorLog(ex=2EErrorCode, "Source: " & ex=2ESource & vbCrLf=
& "Message: " & ex=2EMessage)
=09=09=09End With
=09=09End Try

=09=09Exit Sub

=09End Sub=09'StartListening


--------------------------------
From: Jimmy Chapman

-----------------------
Posted by a user from =2ENET 247 (http://www=2Edotnet247=2Ecom/)

Socket Listener Issue anonymous NO[at]SPAM discussions.microsoft.com
8/10/2004 5:52:09 PM
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]
using a vb.net synchrounous listener to allow a client to
connect.
[quoted text, click to view]
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]
connection state and restart the listener if it detects a
disconnect?
[quoted text, click to view]
(0, "Source: " & ex.Source & vbCrLf & "Message: " &
ex.Message)
[quoted text, click to view]
(ex.ErrorCode, "Source: " & ex.Source & vbCrLf
& "Message: " & ex.Message)
[quoted text, click to view]
AddThis Social Bookmark Button