a reliable way of detecting the next free Port to communicate via winsock ?
Printable View
a reliable way of detecting the next free Port to communicate via winsock ?
It works great with an array of winsock controls.
Have the main one winsock as Winsock1(0) <-- index 0
Then load one or more of the control.
Whenever you want the next available local port
it'd be winsock1(0).localport
or just loop through about 10 winsock controls and try to connect on each port, if the port isn't connected or error then you'll be able to connect to it.
i didnt quite understand. you mean the Next freePort is the Port of the Zero-indexed Winsock ?? or does winsock1(0).localport automatically returns the Next free Port ???
Quote:
Then load one or more of the control.
Whenever you want the next available local port
it'd be winsock1(0).localport
Here's some code that might do what you want:
The first port it encounters without an error is the one it's assigned. I admit this is a kinda crude way to do it, and it won't translate to VB.NET, but it works for everything elseCode:Dim i as Long
On Error Goto loop
For i = 1 To 65535
Winsock1.LocalPort = i
Exit For
loop:
Err.Clear
Next i
' ...whatever with the free port