0

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?

Optional Option
  • 1,521
  • 13
  • 33
  • Have you announced networking capability of your UWP app? https://learn.microsoft.com/en-us/windows/uwp/networking/networking-basics – Lex Li Dec 27 '22 at 01:02
  • Could you please share more information about the port or the address that you are using? Is it a local host address? – Roy Li - MSFT Dec 27 '22 at 01:06
  • It is localhost, port 49002, UWP app has the following networking capabilities: Internet (Client) and Internet (Client & server) – Optional Option Dec 27 '22 at 03:23

1 Answers1

0

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.

Roy Li - MSFT
  • 8,043
  • 1
  • 7
  • 13
  • Enabling the local network loopback link shows the way to enable access for developer but what about the regular user? Can one get the app from the store and have it running locally and receiving UDP data from localhost without jumping through the hoops? – Optional Option Dec 27 '22 at 16:52
  • @OptionalOption This is the limitation for UWP apps. Every user needs to enable the local loop back if the app wants to talk to the local host. I'd suggest you use desktop bridge to do this if you want to bypass the limitation. You could create a desktop app like WPF, which could use the UDP client. Then package the desktop app together with your UWP app. Using app service, you could take between the UWP app and the WPF app. – Roy Li - MSFT Dec 28 '22 at 02:17
  • @OptionalOption Stafen's blog here: https://stefanwick.com/2018/04/16/uwp-with-desktop-extension-part-3/ have a clear explanation about how to implement this. – Roy Li - MSFT Dec 28 '22 at 02:19
  • Great info. Thank you. The desktop extension idea is a terrible hack. It is not serious. Porting the app to WPF would make more sense. Just one more indication that UWP is a dead end. Not worth investing any time – Optional Option Dec 28 '22 at 03:07