Hey gang, I need your help. I'm an old VB 6 programmer trying to make the switch to VB 2008. I'm needing to write an app that can read multiple ports on my local machine and send an ACK back. It's an HL7 listener, if you're familiar we hospital work. I've got the app working great with one port, but I don't see how I can make it listen to multiple ports. Here's the code.....
************************
I need to be able to monitor multiple ports, like 10001, 10002, 10005,......
Please advise and THANKS in advance.
VB.NET:
Private Sub ReadPort(ByVal port As Int32)
' Set the TcpListener on port 13000.
'Dim port As Int32 = 10001
Dim localAddr As IPAddress = IPAddress.Parse("10.2.129.48")
Dim bytes(4096) As [Byte]
Dim data As [String] = Nothing
Dim mystring As String
Dim server As New TcpListener(localAddr, port)
server.Start()
' Perform a blocking call to accept requests.
' You could also user server.AcceptSocket() here.
Dim client As TcpClient = server.AcceptTcpClient()
data = Nothing
Dim stream As NetworkStream = client.GetStream()
While True
If stream.CanRead Then
Dim myReadBuffer(client.ReceiveBufferSize) As Byte
Dim myCompleteMessage As StringBuilder = New StringBuilder()
Dim numberOfBytesRead As Integer = 0
ListBox1.Items.Add("Incoming message on Port: " & port)
Me.Refresh()
My.Application.DoEvents()
' Incoming message may be larger than the buffer size.
Do
numberOfBytesRead = stream.Read(myReadBuffer, 0,
myReadBuffer.Length)
myCompleteMessage.AppendFormat("{0}", Encoding.ASCII.GetString
(myReadBuffer, 0, numberOfBytesRead))
Loop While stream.DataAvailable
mystring = myCompleteMessage.ToString
Else
Console.WriteLine("Sorry. You cannot read from this NetworkStream.")
End If
ListBox1.Items.Add("Received " & mystring.Length & " characters from
Port " & port)
Me.Refresh()
Dim i As Int32
data = mystring.ToUpper()
Dim mysplit As String()
mysplit = data.Split("|")
FileOpen(2, "c:\test\" & MsgID & ".txt", OpenMode.Output)
Print(2, data)
FileClose(2)
mystring = ParseMsg()
Dim msg As [Byte]() = System.Text.Encoding.ASCII.GetBytes(mystring)
ListBox1.Items.Add("Sending ACK back for MsgID: " & MsgID & " to Port "
& port)
Me.Refresh()
stream.Write(msg, 0, msg.Length)
ListBox1.Items.Add("")
Sleep(1000)
End While
End Sub
************************
I need to be able to monitor multiple ports, like 10001, 10002, 10005,......
Please advise and THANKS in advance.