1

I am currently writing a program that listens on UDP 67 (DHCP). For some reason, there seems to be a delay of 1-2 minutes to receive datagrams - but only on WinForms. When executing in a console applications, everything works as intended, with no noticeable delay ( < 10 seconds). I'm using the identical code for each.

Edit: Also note, that sometimes the message doesn't come in for over 5 minutes. Restart the program, it might come in.

Second Edit: Wireshark shows traffic coming in - but my application doesn't ever get it.

Any ideas?

Console Code:

    private const int listenPort = 67;
    private static void StartListener()
    {
        bool done = false;
        UdpClient listener = new UdpClient(listenPort);
        IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, listenPort);
        try
        {
            while (!done)
            {
                Console.WriteLine("Waiting for broadcast");
                byte[] bytes = listener.Receive(ref groupEP);
                int len = bytes.Length;
            }
        }
        catch (Exception e)
        {
            Console.WriteLine(e.ToString());
        }
        finally
        {
            listener.Close();
        }
    }
    public static int Main()
    {
        StartListener();
        return 0;
    }

WinForms Code:

    private const int listenPort = 67;
    public DHCPServer()
    {
        StartListener();
    }
    private void StartListener()
    {
        bool done = false;
        UdpClient listener = new UdpClient(listenPort);
        IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, listenPort);
        try
        {
            while (!done)
            {
                byte[] bytes = listener.Receive(ref groupEP);
                int len = bytes.Length;
            }
        }
        catch (Exception e)
        {   
        }
        finally
        {
            listener.Close();
        }
    }
Mike Christiansen
  • 1,104
  • 2
  • 13
  • 30
  • 10 seconds already is a _very_ noticeable delay! How do you determine when exactly a message has arrived (I assume a breakpoint in the line that follows `Receive()`)? – C.Evenhuis Sep 26 '15 at 18:02
  • @C.Evenhuis - Yes, 10 seconds is a noticeably delay. However, it is within reason for the DHCP process. At least in my opinion. – Mike Christiansen Sep 26 '15 at 18:18
  • @MikeChristiansen can you test your winforms code with `Task.Run(() => StartListener());` – Eser Sep 26 '15 at 19:17
  • @C.Evenhuis Hmm.. Figured out that was not the issue. My program is listening on the wrong network adapter. Wireshark and the VM that I am using for ipconfig /renew are using "interface 5", and my program is using "interface 1" and "interface 6" (both for some reason, probably virtual switch and such). Next question is how do I get my program to listen on the appropriate interface? – Mike Christiansen Sep 26 '15 at 19:51
  • http://stackoverflow.com/questions/7238096/tcplistener-how-to-listen-on-specific-port-on-all-interfaces – Smack Jack Sep 27 '15 at 07:38
  • @SmackJack - Yeah, I tried that, didn't work. I'm not worried about it right now... – Mike Christiansen Sep 29 '15 at 20:20

0 Answers0