07-07-2012, 01:20 PM
Join Date: Apr 2004
Location: Nipomo, California
Determining if UDP port is bound
Scenario: A range of allowed ports is given, for example 1000 to 1009. Multiple connections are allowed, but each must be on its own port, and no specific order per connection is defined or guessable.
Annoying Try/Catch solution:
Private Sub GetEndPoint(Host As IPAddress, Optional Port As Integer = 1000)
If Host IsNot Nothing Then
udpEndPoint = New IPEndPoint(Host, Port)
If Not sckUDP.Bind(udpEndPoint) Then GetEndPoint(Host, Port + 1)
Catch ex As Exception
GetEndPoint(Host, Port + 1)
Dim hostIP As Net.IPAddress = Array.Find(
Function(ipList As IPAddress) ipList.AddressFamily = AddressFamily.InterNetwork)
Is there a clean way to detect if the port is bound without relying on error handling? Something like "netstat -a -p UDP"?
Love & Peace,
Document my code? Why do you think they call it code?