Question

I want to exchange information via UDP from one machine to the other. However, for some reason, it does not work.

This code runs on machine 1, the receiver (192.168.200.1):

class Program
{
    static void Main(string[] args)
    {            
        StartListening();
        Console.Write("Waiting.... Press any key to stop");
        Console.ReadLine();
    }

    private async static void StartListening()
    {
        Console.WriteLine("Start listening...");
        string message = await ReadUdpPacket();
        Console.Write(message);
    }

    private async static Task<string> ReadUdpPacket()
    {
        UdpClient udpClient = new UdpClient(44444);            
        while (udpClient.Available == 0)
        {
            Thread.Sleep(10);
        }
        return "Something received...";
    }
}

and this code runs on machine 2 (192.168.200.2):

static void Main(string[] args)
    {
        Console.WriteLine("Small delay so that listeners can prepare...");
        Thread.Sleep(5000);
        Console.WriteLine("Sending packet...");
        IPEndPoint local = new IPEndPoint(IPAddress.Parse("192.168.200.2"), 0);
        IPEndPoint remote = new IPEndPoint(IPAddress.Parse("192.168.200.1"), 44444); // When changed to 192.168.200.2 and run receiver local, it works
        UdpClient udpClient = new UdpClient(local);
        byte[] bytes = Encoding.ASCII.GetBytes("abcdefghijklmnopqrstuvwxyz");
        udpClient.Send(bytes, bytes.Length, remote);
        Console.Write("Done, press any key...");
        Console.ReadKey();
    }

When I start the programs on both machines, I expected to see 'Something received' appearing in the console window on machine 1. However, that does not happen. I see the UDP packet leaving machine 2 by means of WireShark. The packet is also seen by machine 1; I also run WireShark on that one. But the application on machine 1 does not show a response.

I ran both programs on the same computer (192.168.200.2, thus machine 2) as well. Program 1 can be started, but program 2 needs a slight modification: The line: IPEndPoint remote = new IPEindpoint... needs to contain the local machine's IP address of course; I indicated this by a comment in program 2. So the line reads:

IPEndPoint remote = new IPEndPoint(IPAddress.Parse("192.168.200.2"), 44444);

When I do this, the string 'Something received' arrives!

So it looks that the programming is correct (please note that I minimized the code; this was the smallest project to reproduce the issue that I have). Otherwise I would not see it working with both programs on one machine. The sender application is probably also correct, because the UDP packet goes from one machine to the other (at least, I see the UDP packet on WireShark output which runs on both machines). But anyway, the receiver does not get the packet when it runs on the other machine.

When you run the receiver for the first time, you will most probably get a firewall warning. I clicked 'Allow'.

I also tried both programs on the other machine (after updating the addresses). It also works here. From and to same machine works, from one machine to the other: nothing.

I'm really stuck with this. Anyone a clue on this? Have I forgot anything? I'm running the console apps on Win7 64-bit on both machines. I use VS 2012.

Was it helpful?

Solution

I've found out that have to add two rules to the firewall:

  1. One for your application; so you have to specify the name
  2. One for the port and protocol

If you add both, it works. If you add only one of them, it does not work. I assumed that adding the name of your application should be enough.

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