Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / .NET SDK / August 2004

Tip: Looking for answers? Try searching our database.

Socket Listener Issue

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jimmy Chapman - 09 Aug 2004 18:34 GMT
I have a problem with my TCP Socket Listener.  I am using a vb.net synchrounous listener to allow a client to connect.

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.  

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?

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)
            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)
            End With
        End Try

        Exit Sub

    End Sub    'StartListening
- 11 Aug 2004 01:52 GMT
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

>-----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.

>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.  

>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?

>I have attached the following code below.  The public sub procedure is called from a Windows Service.
>
[quoted text clipped - 49 lines]
>                    For Each strChar In aryMsg
>                        If
strChar = "MSA" Then
>                       
    strNewStr = strNewStr & Chr(13) & strChar & "|"
>                        Else
>                       
    strNewStr = strNewStr & strChar & "|"
>                        End If
>                    Next
[quoted text clipped - 11 lines]
>                    'Set variables to Nothing
>                    strNewStr =
Nothing
>                    strNewMSA =
Nothing
>                    strResponse =
Nothing
>                    strReplace =
Nothing
>                    aryMsg = Nothing
>                    msg = Nothing
[quoted text clipped - 13 lines]
>            With clsError
>                .CreateErrorLog(0, "Source: " & ex.Source & vbCrLf & "Message: " &
ex.Message)
>            End With
>
[quoted text clipped - 3 lines]
>            With clsError
>                .CreateErrorLog
(ex.ErrorCode, "Source: " & ex.Source & vbCrLf
& "Message: " & ex.Message)
>            End With
>        End Try
[quoted text clipped - 8 lines]
>-----------------------
>Posted by a user from .NET 247
(http://www.dotnet247.com/)

><Id>B49U81iiUUuurOcpILQT7A==</Id>
>.

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.