1
votes

I'm struggling to get a dotnet core console application to receive a UDP message. When I use the same code in a dotnet framework console app, the messages are received, so I feel nothing should be blocking them (ie. firewall, etc.).

I've tried running this project using Visual Studio, and published a release version and run using dotnet in a command window, but nothing is received. No exceptions. It seems so simple. I'm at a loss. Any advice is appreciated. Thanks.

This is the code:

static void Main(string[] args)
{
    var client = new UdpClient(10006);
    var ipEndPoint = new IPEndPoint(IPAddress.Any, 0);

    var msgReceived = client.Receive(ref ipEndPoint);
    Console.WriteLine($"received '{Encoding.ASCII.GetString(msgReceived)}'");
}

I see the same/similar question asked here, without an answer: How to send and receive commands from a UDP network server via .NET Core console application

1

1 Answers

0
votes

Are you sure you are actually sending data to that machine and port number. The code should work ok. I put this in my Main and it receives the data. I used threads so they can be in the same process, I didnt test it as seperate processes.

var threadReceiver = new Thread(() =>
{
    var client = new UdpClient(10006);
    var ipEndPoint = new IPEndPoint(IPAddress.Any, 0);

    var msgReceived = client.Receive(ref ipEndPoint);
    Console.WriteLine($"received '{Encoding.ASCII.GetString(msgReceived)}'");
});



var threadSender = new Thread(() =>
{
    var client = new UdpClient(10007);
    var ipEndPoint = new IPEndPoint(IPAddress.Any, 0);
    var bytes = Encoding.ASCII.GetBytes("this is the data");
    Thread.Sleep(1000);
    var msgReceived = client.Send(bytes, bytes.Length, new IPEndPoint(IPAddress.Parse("127.0.0.1"), 10006)) ;
    Console.WriteLine($"Sent data");
});

threadReceiver.Start();
threadSender.Start();

threadReceiver.Join();

This outputs :

Sent data
received 'this is the data'

When i ran this the first time I was asked by windows firewall to allow the traffic. I checked private and public networks. You need to make sure your firewall is enabled for the network type you're connected to. Check the network settings to see if windows thinks is public or private. You can setup firewall rules manually for the app in the Windows firewall settings.