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.GetHostEntry(Dns.GetHostName())
Dim ipAddress As IPAddress = ipHostInfo.AddressList(1)
Dim localEndPoint As New IPEndPoint(ipAddress, 11000)
' Create a TCP/IP socket.
Dim listener As New Socket(AddressFamily.InterNetwork, _
SocketType.Stream, ProtocolType.Tcp)
'I added this but it didnt work.
listener.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.KeepAlive, 1)
' Bind the socket to the local endpoint and
' listen for incoming connections.
listener.Bind(localEndPoint)
listener.Listen(0)
' Start listening for connections.
While True
Console.WriteLine("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.StringToHex(data).IndexOf("1C0D") -1
Then
Exit While
End If
End If
End While
' Show the data on the console.
Console.WriteLine("Text received : {0}", data)
' Echo the data back to the client.
Dim msg As Byte() = Encoding.ASCII.GetBytes(buildAck(data))
handler.Send(msg)
handler.Shutdown(SocketShutdown.Send)
handler.Close()