Question

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:

wireshark capture

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?

Was it helpful?

Solution

In the end we could not resolve this issue. It was not limited to Asynchronous IO or completion ports. Finally we took the work around step of using PcapDotNet to pick up the packets directly, which worked.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top