0

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?

James
  • 9,774
  • 5
  • 34
  • 58
  • Could it be that when you do get an ObjectDisposedException, you never start listening again for that socket ? I think you need to bypass "do stuff with data" but still do another BeginReceive ? – andrew Jul 15 '13 at 11:04
  • @andrew, at that point the socket has been disposed and `OpenUDPSocket()` is called again by the user. – James Jul 15 '13 at 11:34
  • what happens if SocketException is thrown in `data.Socket.EndReceive(ar);`. you're not handling it seems – Sriram Sakthivel Jul 15 '13 at 11:40
  • @SriramSakthivel, That's true, but no exceptions occur. – James Jul 15 '13 at 12:00
  • it is strange that restarting app doesn't helped you. there is something else going wrong. – Sriram Sakthivel Jul 15 '13 at 12:29
  • I'm just guessing here - if you make a call to OpenUDPSocket again - the first thing that does is processDataSockets.Clear - so does your data record become unreferenced if it's no longer in the list and not in another scope ? – andrew Jul 15 '13 at 12:54
  • @andrew, That's correct, any data from previous connections is discarded. – James Jul 15 '13 at 13:08

1 Answers1

0

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.

James
  • 9,774
  • 5
  • 34
  • 58