简体   繁体   English

通用Windows App从任何服务器接收端口上的UDP数据

[英]Universal Windows App receiving UDP data on a port from any server

I am trying to implement an UDP listener on a specific port but from any machine. 我正在尝试从任何计算机的特定端口上实现UDP侦听器。 I am trying to use the "new" Universal Windows project using Visual Studio 2015. 我正在尝试通过Visual Studio 2015使用“新的”通用Windows项目。

Using a WPF "old" type of project I could do the following: 使用WPF“旧”类型的项目,我可以执行以下操作:

public void StartListening()
{
    this.client = new System.Net.Sockets.UdpClient(5606);
    this.endpoint = new System.Net.IPEndPoint(System.Net.IPAddress.Any, 5606);
    this.client.BeginReceive(new AsyncCallback(receive), this);
}

private static void receive(IAsyncResult result)
{
    var self = ((UDPListener)result.AsyncState);
    var receivedBytes = self.client.EndReceive(result, ref self.endpoint);
    // do something with receivedBytes
    self.StartListening();
}

However using Universal Windows it seems to be quite different. 但是,使用Universal Windows似乎大不相同。 There is no System.Net.Sockets.UdpClient any more. 没有任何System.Net.Sockets.UdpClient了。 The only thing I can find is connecting to/from a client/server UDP things and stuffs using Windows.Networking.Sockets.DatagramSocket . 我唯一能找到的是使用Windows.Networking.Sockets.DatagramSocket与客户端/服务器UDP连接的东西。 With which I came up with the following: 我提出了以下建议:

public async void Connect()
{
    var listenerSocket = new Windows.Networking.Sockets.DatagramSocket();

    listenerSocket.MessageReceived += ListenerSocket_MessageReceived;

    await listenerSocket.BindServiceNameAsync("5606");
}

private void ListenerSocket_MessageReceived(Windows.Networking.Sockets.DatagramSocket sender, Windows.Networking.Sockets.DatagramSocketMessageReceivedEventArgs args)
{
    throw new NotImplementedException();
}

But this does not seem to do what I want. 但这似乎不符合我的要求。 I never receive any data from a server that is running in the background. 我从来没有从后台运行的服务器接收任何数据。 Where the WPF version does receive data. WPF版本接收数据的位置。

What am I doing wrong? 我究竟做错了什么? Is this not even possible any more? 这是不可能了吗? Can Universal Windows applications only receive data from other Universal Windows applications? 通用Windows应用程序只能从其他通用Windows应用程序接收数据吗? Or am I just looking at the wrong things here? 还是我只是在看错东西?

With UWP, they have a so-called "network isolation" mechanism that blocks UWP apps from doing networking with other apps on the same machine. 使用UWP,它们具有所谓的“网络隔离”机制,可阻止UWP应用与同一台计算机上的其他应用进行联网。 They have tooling (and registry settings) to enable per-app loopback exceptions, but those are only available for client (not listening) sockets at the UWP app side. 它们具有工具(和注册表设置)来启用按应用程序环回异常,但这些异常仅适用于UWP应用程序端的客户端(非监听)套接字。

(Shot themselves in the foot again) (再次射中自己的脚)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM