I am trying to catch some broadcast packets from some other devices on the network. When I try to get the socket.RemoteEndPoint so I can get the IP address of the device that sent the broadcast I get and error. If I remove that line it continues on as it should and the data from the device is readable.
What am I doing wrong?
Exception System.InvalidOperationException - CLR_E_INVALID_OPERATION (8)
Message:
GHIElectronics.TinyCLR.Devices.Network.Provider.NetworkControllerApiWrapper::GetRemoteAddress [IP: 0000]
System.Net.Sockets.Socket::GetEndPoint [IP: 003d]
System.Net.Sockets.Socket::get_RemoteEndPoint [IP: 0005]
int port = 5556;
var ipEndPoint = new IPEndPoint(IPAddress.Any, port);
var socket = new Socket(System.Net.Sockets.AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
byte[] data = new byte[1024];
string name;
try
{
socket.Bind(ipEndPoint);
while (true)
{
while(!socket.Poll(10,SelectMode.SelectRead))
{
Thread.Sleep(10);
}
var available = socket.Available;
Debug.WriteLine("Network data: " + available.ToString() );
if (available == 0) break;
Debug.WriteLine("Network: " + socket.RemoteEndPoint.ToString());//<----Error happens here.
socket.Receive(data);
name = Encoding.UTF8.GetString(data, 374, 24);
Debug.WriteLine(name);
}
}
catch (Exception ex)
{
Debug.WriteLine("Network: " + ex.Message);
}