Win sock connect with multiple port

RETAILREALM
RETAILREALM used Ask the Experts™
on
Hi
We are using Winsock control to connect to external program whose  ip address let’s say 192.168.1.1
Now we need to talk with that control on port 25000
This is fine as we are connected to the same port

    TCPClient.RemoteHost = "192.168.1.1"
    TCPClient.RemotePort = 25000
   TCPClient.Connect

But at the same time external programmer is broadcasting some messages on port 25001 , how can we listen to that port ( we don’t need to send data to this port only listen ) using the same winsock or there is any other method.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2009

Commented:
I do the same thing in my programs. You will need an array of WinSocks, not just one. A WinSock can only service one port number at a time. In the WinSocks's DataArrival event, you can then use the Index parameter to decide what to do with the data.

Author

Commented:
Hi

Thanks for your reply

Can you please explain this  , as we are still not able to do so , even after doing control array
Top Expert 2009
Commented:
Well, once you make an array of WinSocks, I use something like this to initialize them:

Let's say you have four WinSocks...

For i = 0 to 3
   WinSock1(i).Close
   WinSock1(i).Protocol = sckUDPProtocol
   WinSock1(i).RemoteHost = "172.0.0.1"
   WinSock1(i).RemotePort = 80 + i   'use any port number here, this is just an example
   WinSock1(i).Bind 0, "172.0.0.2"
Next i%

Then your DataArrival event might look something like this:

Private Sub WinSock1_DataArrival(Index As Integer, ByVal bytesTotal As Long)
    Dim mem As String

   On Local Error Resume Next
   WinSock1(Index).GetData mem, vbString, bytesTotal
   On Local Error GoTo 0
     
   Select Case Index
      Case 0
         'Do something with the data in the mem string for your first port
      Case 1
         'Do something with the data in the mem string for your second port
      Case 2
         'Do something with the data in the mem string for your third port
      Case 3
         'Do something with the data in the mem string for your fourth port
   End Select
   
End Sub

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial