
07-07-2012, 01:20 PM
|
 |
Contributor
|
|
Join Date: Apr 2004
Location: Nipomo, California
Posts: 456
|
|
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:
Code:
Private Sub GetEndPoint(Host As IPAddress, Optional Port As Integer = 1000)
If Host IsNot Nothing Then
udpEndPoint = New IPEndPoint(Host, Port)
Try
If Not sckUDP.Bind(udpEndPoint) Then GetEndPoint(Host, Port + 1)
Catch ex As Exception
GetEndPoint(Host, Port + 1)
End Try
End If
End Sub
Call:
Code:
Dim hostIP As Net.IPAddress = Array.Find(
Dns.GetHostEntry(Dns.GetHostName).AddressList,
Function(ipList As IPAddress) ipList.AddressFamily = AddressFamily.InterNetwork)
GetEndPoint(hostIP)
Question:
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,
RealityRipple
Document my code? Why do you think they call it code?
|