1

In my Xamarin.Forms app for UWP I have the following simple code to receive a UDP datagram:

using (var udpClient = new UdpClient(port))
{
  var datagram = await udpClient.ReceiveAsync();
}

For testing purposes I need to run this app from within Visual Studio and from the same computer send a datagram to the app with some (non-UWP) terminal program. But it doesn't work - ReceiveAsync() never returns.

I know that UWP prohibits loopback communication by default, but for debugging it should be possible according to https://stackoverflow.com/a/44990978/487356.

In the UWP project I enabled the "Internet (Client)", "Internet (Client & Server)" and "Private Networks (Client & Server)" capabilities. Also I enabled "Allow local network loopback" in the debug settings. And using the CheckNetIsolation tool I was able to verify that my app is listed under LoopbackExempt. So this all looks good, but I still can't receive any UDP datagrams.

Is there something else I need to do for this to work?


Edit Reading a bit more about this, it looks like receiving data from the same machine is prohibited even when the app is listed under LoopbackExempts, see Problems with UDP in windows 10. UWP. This is really annoying!

So I guess the follow-up question would be: What is a good (preferrably connection-less) alternative for IPC?

Robert Hegner
  • 9,014
  • 7
  • 62
  • 98
  • What your understanding is correct. 'Network communications using an IP loopback address cannot be used for interprocess communication (between two different apps) in a Windows Runtime app since this is restricted by network isolation.' **'But Network communication using an IP loopback address is allowed within an app within the same process for communication purposes'**. So, if it's for testing purposes, you could do like the official UWP [DatagramSocket sample](https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/DatagramSocket). Listening and connect in the same app. – Xie Steven Jan 25 '19 at 03:06

1 Answers1

1

For testing purposes I need to run this app from within Visual Studio and from the same computer send a datagram to the app with some (non-UWP) terminal program. But it doesn't work - ReceiveAsync() never returns.

If this is just for testing purposes, you can use CheckNetIsolation tool to temporarily enable receiving connections from the same machine:

CheckNetIsolation.exe LoopbackExempt -is -n=<AppContainer or Package Family>

However, as documented here, the tool will have to be continuously running as admin. We use this mechanism for unit testing our code and it works very well.

Sunius
  • 2,789
  • 18
  • 30
  • This did the trick, thanks! Also it really depends what port you're using. I had to try a few until a found one that worked. – Robert Hegner Jan 29 '19 at 16:44