I have a listener that I am using and am trying to receive info from a company with an HL-7 broker. He is able to connect to my listener and I actually receive data but the message should be an HL-7 message with several segments terminated by a carriage return after each. I seem to be receiving the carriage returns but nothing else. Here is my code below. Any suggestions on what I need to try? P.S. - If I set up a client using .net sockets and send using the second group of code below, the message comes across perfectly.
'-- SERVER CODE STARTS HERE --
' -- CREATE SOCKET
Dim clsEndpoint As Net.IPEndPoint = New Net.IPEndPoint(New Net.IPAddress(New Byte() {192, 168, 0, 11}), 11000)
Dim clsServerSocket As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
' -- BIND TO I.P. AND PORT - this blocks other listeners from using the same combination...
clsServerSocket.Bind(clsEndpoint)
' -- LISTEN FOR INCOMING CONNECTIONS
clsServerSocket.Listen(CInt(Fix(SocketOptionName.MaxConnections)))
Do
' -- POLL FOR CONNECTIONS - this allows us to keep listening so the thread doesn't block...
If clsServerSocket.Poll(10000, SelectMode.SelectRead) Then
' -- A MESSAGE IS BEING SENT - create a socket to read it...
Dim clsSocket As Socket = clsServerSocket.Accept()
' -- READ THE MESSAGE FROM THE BUFFER
Dim bReadBuffer As Byte() = New Byte(clsSocket.Available) {}
clsSocket.Receive(bReadBuffer, bReadBuffer.Length, SocketFlags.None)
' -- CONVERT DATA TO A STRING
Dim sMessage As String = ""
sMessage = System.Text.Encoding.ASCII.GetString(bReadBuffer)
' -- DISPLAY MESSAGE
lbSentData.Items.Add(sMessage & vbCrLf)
MsgBox("Message Received", MessageBoxButtons.OK, MessageBoxIcon.Information)
' -- RELEASE SOCKET
clsSocket.Close()
clsSocket = Nothing
End If
Loop
'-- CLIENT CODE STARTS HERE --
Try
Dim clsError As System.Net.Sockets.SocketError
Dim bMessage As Byte() = System.Text.Encoding.ASCII.GetBytes(DataToSend)
Dim clsSocket As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
Dim clsEndpoint As Net.IPEndPoint = New Net.IPEndPoint(New Net.IPAddress(New Byte() {192, 168, 0, 102}), 8000)
clsSocket.Connect(clsEndpoint)
clsSocket.Send(bMessage, 0, bMessage.Length, SocketFlags.None, clsError)
If clsError = SocketError.Success Then
MessageBox.Show(Me, "Message sent!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information)
Else
MessageBox.Show(Me, "The message was not sent successfully, the message was: " & clsError.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
Catch ex As Exception
MessageBox.Show(Me, ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
'-- SERVER CODE STARTS HERE --
' -- CREATE SOCKET
Dim clsEndpoint As Net.IPEndPoint = New Net.IPEndPoint(New Net.IPAddress(New Byte() {192, 168, 0, 11}), 11000)
Dim clsServerSocket As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
' -- BIND TO I.P. AND PORT - this blocks other listeners from using the same combination...
clsServerSocket.Bind(clsEndpoint)
' -- LISTEN FOR INCOMING CONNECTIONS
clsServerSocket.Listen(CInt(Fix(SocketOptionName.MaxConnections)))
Do
' -- POLL FOR CONNECTIONS - this allows us to keep listening so the thread doesn't block...
If clsServerSocket.Poll(10000, SelectMode.SelectRead) Then
' -- A MESSAGE IS BEING SENT - create a socket to read it...
Dim clsSocket As Socket = clsServerSocket.Accept()
' -- READ THE MESSAGE FROM THE BUFFER
Dim bReadBuffer As Byte() = New Byte(clsSocket.Available) {}
clsSocket.Receive(bReadBuffer, bReadBuffer.Length, SocketFlags.None)
' -- CONVERT DATA TO A STRING
Dim sMessage As String = ""
sMessage = System.Text.Encoding.ASCII.GetString(bReadBuffer)
' -- DISPLAY MESSAGE
lbSentData.Items.Add(sMessage & vbCrLf)
MsgBox("Message Received", MessageBoxButtons.OK, MessageBoxIcon.Information)
' -- RELEASE SOCKET
clsSocket.Close()
clsSocket = Nothing
End If
Loop
'-- CLIENT CODE STARTS HERE --
Try
Dim clsError As System.Net.Sockets.SocketError
Dim bMessage As Byte() = System.Text.Encoding.ASCII.GetBytes(DataToSend)
Dim clsSocket As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
Dim clsEndpoint As Net.IPEndPoint = New Net.IPEndPoint(New Net.IPAddress(New Byte() {192, 168, 0, 102}), 8000)
clsSocket.Connect(clsEndpoint)
clsSocket.Send(bMessage, 0, bMessage.Length, SocketFlags.None, clsError)
If clsError = SocketError.Success Then
MessageBox.Show(Me, "Message sent!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information)
Else
MessageBox.Show(Me, "The message was not sent successfully, the message was: " & clsError.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
Catch ex As Exception
MessageBox.Show(Me, ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try