Maintain Persistant Socket Connection

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • T1Stuff

    #1

    Maintain Persistant Socket Connection

    I have programmed a simple synchronous socket server app in vb.net to
    communicate and receive messages from our data integration tool that works
    well with all our other systems. The program I built works with as many
    messages as I can send accross as long as they are sent one after another
    (queued). However once any length of time passes, the server app doesn't
    receive any data as if the listener doesnt have the connection any more. I
    have to reset the sending system, then it can receive data again. Is there
    a way to maintain a persistant connection on a socket connection?

    Thanks.
    Any help would be appriciated.

    Below is the code, most of which comes from the MSDN example.

    Dim bytes() As Byte = New [Byte](1024) {}

    ' Establish the local endpoint for the socket.
    ' Dns.GetHostName returns the name of the
    ' host running the application.
    Dim ipHostInfo As IPHostEntry = Dns.GetHostEntr y(Dns.GetHostNa me())
    Dim ipAddress As IPAddress = ipHostInfo.Addr essList(1)
    Dim localEndPoint As New IPEndPoint(ipAd dress, 11000)

    ' Create a TCP/IP socket.
    Dim listener As New Socket(AddressF amily.InterNetw ork, _
    SocketType.Stre am, ProtocolType.Tc p)
    'I added this but it didnt work.
    listener.SetSoc ketOption(Socke tOptionLevel.So cket,
    SocketOptionNam e.KeepAlive, 1)

    ' Bind the socket to the local endpoint and
    ' listen for incoming connections.
    listener.Bind(l ocalEndPoint)
    listener.Listen (0)
    ' Start listening for connections.
    While True
    Console.WriteLi ne("Waiting for a connection...")
    ' Program is suspended while waiting for an incoming connection.
    Dim handler As Socket = listener.Accept ()
    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 Not data Is DBNull.Value Then
    'This does some work on the message checking for the end
    "1C0D"
    If HL7Toolbox.Stri ngToHex(data).I ndexOf("1C0D") -1
    Then
    Exit While
    End If
    End If
    End While
    ' Show the data on the console.
    Console.WriteLi ne("Text received : {0}", data)
    ' Echo the data back to the client.
    Dim msg As Byte() = Encoding.ASCII. GetBytes(buildA ck(data))
    handler.Send(ms g)

    handler.Shutdow n(SocketShutdow n.Send)
    handler.Close()

Working...