I've been trying to setup a client server between a UWP app as the client and a .NET desktop app as the server. I'm using UDP Datagrams as the messaging system between the two.
Here my UWP code to listen for Datagrams on the localhost IP at port 22222:
private async void listenToServer()
{
// Setup UDP Listener
socketListener = new DatagramSocket();
socketListener.MessageReceived += MessageReceived;
await socketListener.BindEndpointAsync(new HostName("127.0.0.1"),"22222");
Debug.WriteLine("Listening: " + socketListener.Information.LocalAddress + " " + socketListener.Information.LocalPort);
}
private async void MessageReceived(DatagramSocket sender, DatagramSocketMessageReceivedEventArgs args)
{
// Interpret the incoming datagram's entire contents as a string.
uint stringLength = args.GetDataReader().UnconsumedBufferLength;
string receivedMessage = args.GetDataReader().ReadString(stringLength);
Debug.WriteLine("Received " + receivedMessage);
}
Here is my WinForm .NET desktop app to send Datagrams on localhost on port 2222:
public async void sendToClient()
{
// Setup UDP Talker
talker = new UdpClient();
sending_end_point = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 22222);
talker.Connect(sending_end_point);
byte[] send_buffer = Encoding.ASCII.GetBytes("Hello!");
await talker.SendAsync(send_buffer, send_buffer.Length);
}
Here is what I've tried, and what I know from my troubleshooting:
Sending a UDP datagram from UWP to .NET desktop works.
UWP code to send message to .NET desktop over localhost port 11111:
public async void sendToServer() { // Connect to the server socketTalker = new DatagramSocket(); await socketTalker.ConnectAsync(new HostName("127.0.0.1"), "11111"); Debug.WriteLine("Connected: " + socketTalker.Information.RemoteAddress + " " + socketTalker.Information.RemotePort); // Setup Writer writer = new DataWriter(socketTalker.OutputStream); writer.WriteString("Ping!"); await writer.StoreAsync(); writer.DetachStream(); writer.Dispose(); }
.NET desktop code to listen for message from UWP over same IP and port:
private async Task listenToClient() { // Setup listener listener = new UdpClient(11111); UdpReceiveResult receiveResult = await listener.ReceiveAsync(); Debug.WriteLine(" Received: " + Encoding.ASCII.GetString(receiveResult.Buffer)); }
Sending UDP Datagram from .NET desktop to UWP works across different IPs (2 different computers)
I've tested this by setting the listener and talker IP addresses to the same IP address where the server was running, and it works flawlessly. This lead to research which got me to #3...
Loopback exemption didn't make a difference
Running CheckNetIsolation.exe and the Loopback exemption tool to exempt the UWP app from loopback IP restriction didn't fix this issue. It seems it shouldn't matter, from what I read (Problems with UDP in windows 10. UWP), running in the Visual Studio environment should already be exempt from loopback, but I tried anyways, and not luck.