I am trying to write a program that will open a UDPClient and listen on it.
I have based my code on the example at MS in the "UDPClient.Receive Method
(.NET Framework)" file, but when I try to call the .Receive method I get the
following exception:
System.Net.Sockets.SocketException: An invalid argument was supplied
at System.Net.Sockets.Socket.ReceiveFrom(Byte[] buffer, Int32 offset,
Int32 size, SocketFlags socketFlags, EndPoint& remoteEP)
at System.Net.Sockets.Socket.ReceiveFrom(Byte[] buffer, Int32 size,
SocketFlags socketFlags, EndPoint& remoteEP)
at System.Net.Sockets.UdpClient.Receive(IPEndPoint& remoteEP)
The relevant code is:
Public Shared Sub StartListener(ByVal address As IPAddress, ByVal port
As Integer)
Dim listener As New UdpClient
Dim remoteEP As New IPEndPoint(IPAddress.Any, 0)
Try
Debug.WriteLine("Waiting for broadcast")
Dim receiveBytes As Byte() = listener.Receive(remoteEP)
Debug.WriteLine("Received broadcast from {0} :" + _
ControlChars.Cr + " {1}" + ControlChars.Cr, _
remoteEP.ToString() + ControlChars.CrLf + "Content: " + _
Encoding.ASCII.GetString(receiveBytes, 0,
receiveBytes.Length))
listener.Close()
Catch e As Exception
Debug.WriteLine(e.ToString())
End Try
End Sub
and the exception happens at the line:
Dim receiveBytes As Byte() = listener.Receive(remoteEP)
Can anyone tell me what I am doing wrong here?
TIA
Ron L