在同一台机器上连接两个本地 uwp 应用程序
我试图让两个 uwp 应用程序(Windows 10 应用程序)在同一台机器上运行时进行通信.在不同主机上运行时,应用程序已经可以通信,因此代码可以正常工作(两个应用程序都可以在其清单文件中声明的本地和公共网络中进行通信).但是,当在同一主机上运行时,客户端应用程序无法连接到服务器.
Im trying to let two uwp apps (windows 10 apps) communicate while running on the same machine. The Apps already can communicate when run on different hosts, so the code is working (both apps are enabled to communicate in local and public networks declared in their manifest files). When running on the same host however, the client application is not able to connect to the server.
我使用 Visual Studio 2015 Community Edition Update 3 进行开发.
Im using Visual Studio 2015 Community Edition Update 3 for developing.
在 ProjectSettings->Debug->Allow local network loopback 下勾选.
Under ProjectSettings->Debug->Allow local network loopback is checked.
我尝试通过命令行(对于两个应用程序)添加 LoopbackExempt:
I tried to ad an LoopbackExempt via commandline (for both apps):
checknetisolation LoopbackExempt -d -n=<packagefamilyname>
但还是不行.
我使用的代码(认为可能不相关)
The code im using (thought might not be relavant)
服务端代码:
var listener = new StreamSocketListener();
listener.ConnectionReceived += Listener_ConnectionReceived1;
await listener.BindServiceNameAsync("20000", SocketProtectionLevel.PlainSocket);
客户端代码:
StreamSocket socket = new StreamSocket();
_hostName = <hostname/ip>;
await socket.ConnectAsync(new HostName(_hostName), "20000",SocketProtectionLevel.PlainSocket);
环回豁免将允许应用作为客户端连接到本地系统,但不会让应用作为服务器接收本地连接.
The loopback exemption will allow the app to connect out to the local system as a client, but it won't let the app receive local connections as a server.
请参阅如何启用环回一文中的 MSDN 注释并解决网络隔离问题(Windows 运行时应用)
注意 环回仅允许用于开发目的.使用在 Visual Studio 之外安装的 Windows 运行时应用程序不是允许.此外,Windows 运行时应用程序只能使用 IP 环回作为客户端网络请求的目标地址.所以一个 Windows使用 DatagramSocket 或 StreamSocketListener 的运行时应用程序侦听 IP 环回地址被阻止接收任何传入数据包.
Note Loopback is permitted only for development purposes. Usage by a Windows Runtime app installed outside of Visual Studio is not permitted. Further, a Windows Runtime app can use an IP loopback only as the target address for a client network request. So a Windows Runtime app that uses a DatagramSocket or StreamSocketListener to listen on an IP loopback address is prevented from receiving any incoming packets.
根据具体需要,还有其他几种选择.最有可能的两个是:
There are several other options depending on what exactly the need is. The most likely two are:
如果目标只是为了测试,那么在不同的系统上运行应用程序.如果目标是 IPC,则实施 应用服务.应用服务专为 UWP 到 UWP 通信而设计
If the goal is only for testing then run the apps on different systems. If the goal is IPC then implement an App Service. App services are specifically designed for UWP to UWP communication
如果您正在侧加载(无论如何您都需要这样做才能调用 checknetisolation),那么您还可以考虑添加一个代理的 Windows 运行时组件或桌面应用程序作为代理服务器,两个客户端都可以连接到,但我肯定会先查看应用服务选项.
If you're side-loading (which you'd need to be doing to call checknetisolation anyway) then you can also look into adding a brokered Windows Runtime Component or a desktop app as a broker server which both clients can connect to, but I'd definitely check out the app service option first.