Optional Option
Optional Option

Reputation: 1571

UdpClient does not receive any data in UWP app

I have a UWP app that needs to use UdpClient to receive some data. The code looks very similar to this:

var udp = new UdpClient(port);
var groupEP = new IPEndPoint(IPAddress.Any, port);
while (true)
{
    Trace.WriteLine("Waiting for broadcast");
    byte[] bytes = udp.Receive(ref groupEP);

    Trace.WriteLine($"Received broadcast from {groupEP} :");
    Trace.WriteLine($" {Encoding.ASCII.GetString(bytes, 0, bytes.Length)}");
}

When I run this code in UWP app it stops at Receive(), does not receive anything, and there are no exceptions.

If I run the same exact code in NET 5 console app everything works fine.

How can I make this code run in UWP app?

Upvotes: 0

Views: 201

Answers (1)

Roy Li - MSFT
Roy Li - MSFT

Reputation: 8681

A common reason for such kind of network issue is the local network loopback. UWP apps are running in the sandbox and are isolated from the system resources like network and file system. In other works, UWP apps are not allowed to access the local host address by default. Enabling the local network loopback could make UWP apps able to access local network resources.

Please also make sure that you've enabled the enterpriseAuthentication and privateNetworkClientServer capability in the manifest file.

Upvotes: 0

Related Questions