.NETGURU
Socket Listener Issue
Messages   Related Types
This message was discovered on microsoft.public.dotnet.framework.sdk.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.
Post a new message to this list...

Jimmy Chapman via .Net Guru (VIP)
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

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

-----------------------
Posted by a user from .Net Guru (http://www.dot-net-guru.com/)

<Id>B49U81iiUUuurOcpILQT7A==</Id>
Reply to this message...
 
    
anonymous@discussions.microsoft.com
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 clipped]

connect.
[Original message clipped]

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.
[Original message clipped]

disconnect?
[Original message clipped]

ex.Message)
[Original message clipped]

& "Message: " & ex.Message)
[Original message clipped]

Reply to this message...
 
 
System.Net.IPAddress
System.Net.IPEndPoint
System.Net.Sockets.NetworkStream
System.Net.Sockets.SocketException
System.Net.Sockets.SocketShutdown
System.Text.Encoding




ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
Ad


Need Dot Net Interview Questions?
Ask ExamGuru, Inc. for advice and help on Passing .Net Interviews
.Net Projects
Best-of-breed application framework for .NET projects, developed by ExamGuru, Inc. and ExamGuru IT
Free .net Help
Commission ExamGuru, Inc. and his team for your next bespoke software project
FogBUGZ
The only bug tracking system carefully crafted with one goal in mind: helping teams create great software.
Awesome Tools
If you don't know about these, you're missing out... IT Certification Questions
IT Interview Questions
Free Oracle 10g Training
MCSE Boortcamp
Cisco Study Guides
Cheap Study Guides
Exact Questions
Dot Net Interview Questions
Oracle OCP
Cheap Travel
Designer Perfumes - Wholesale Prices
Free Programming Tutorials
 
ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
 Copyright © ExamGuru, Inc. 2001-2006
Contact Us - Terms of Use - Privacy Policy - www.dot-net-guru.com - www.examguru.net - www.oraclesource.net - www.itinterviews.net - www.examguru.net/ITCertification