I'm opening a UDP Socket for receiving udp packets. However sometimes It never gets to the point Do stuff with data
.
Data is being received, I can see it on Wireshark:
but the callback only runs to close the socket when I run the Disconnect code.
private void OpenUDPSocket()
{
this.processDataSockets.Clear();
IPHostEntry host;
host = Dns.GetHostEntry(Dns.GetHostName());
foreach (IPAddress ip in host.AddressList)
{
UPDData data = new UPDData();
data.Socket = new Socket(ip.AddressFamily, SocketType.Dgram, ProtocolType.Udp);
data.Socket.Bind(new IPEndPoint(ip, 2222));
data.Socket.EnableBroadcast = true;
data.Buffer = new byte[512];
data.Socket.BeginReceive(data.Buffer, 0, 512, SocketFlags.None, this.ReceivedData, data);
this.processDataSockets.Add(data);
}
this.socketOpen = true;
}
private void ReceivedData(IAsyncResult ar)
{
UPDData data;
try
{
data = (UPDData)ar.AsyncState;
data.Socket.EndReceive(ar);
}
catch (ObjectDisposedException)
{
// The connection has been closed
return;
}
//... Do stuff with data
data.Socket.BeginReceive(data.Buffer, 0, 512, SocketFlags.None, this.ReceivedData, data);
}
When this happens I'm left stuck, restarting the application doesn't help. I need to reboot my machine for the callback to start working again.
I have no idea where to go from here or how to fix this.
Any Ideas what's happening?