TcpListener - How to read multiple ports?

bsbeck

New member
Joined
Oct 7, 2008
Messages
2
Programming Experience
10+
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.....
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.
 
You need one TcpListener for each port, the underlying Socket can only bind to a single local endpoint (ip+port). If it would be more convenient you can accept multiple concurrent connections to the same local port, to do this run the listener in its own thread where it only accept connections, and do communication for ongoing connections in different threads.
 
Let me better explain what I'm trying to do.......

I've got a Meditech Server that sends HL7 messages to my PC on multiple ports. I know the port numbers. There may be 2 or there may be 20, depending on the setup.

1. I need to walk through a list of ports on my local IP address.
2. I need to read from that port and then send an ACK back.


My code does all this for ONE port now, but I'm not seeing how to do it for a list of ports.
 
As said, you need to run one listener for each port. You also have some reading to do regarding learning multi-threading.
 
Back
Top