问题
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.
Im using Visual Studio 2015 Community Edition Update 3 for developing.
Under ProjectSettings->Debug->Allow local network loopback is checked.
I tried to ad an LoopbackExempt via commandline (for both apps):
checknetisolation LoopbackExempt -d -n=<packagefamilyname>
But still not working.
The code im using (thought might not be relavant)
Serverside code:
var listener = new StreamSocketListener();
listener.ConnectionReceived += Listener_ConnectionReceived1;
await listener.BindServiceNameAsync("20000", SocketProtectionLevel.PlainSocket);
Clientside code:
StreamSocket socket = new StreamSocket();
_hostName = <hostname/ip>;
await socket.ConnectAsync(new HostName(_hostName), "20000",SocketProtectionLevel.PlainSocket);
回答1:
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.
See the Note on MSDN in the article How to enable loopback and troubleshoot network isolation (Windows Runtime apps)
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:
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
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.
来源:https://stackoverflow.com/questions/38897774/connecting-two-local-uwp-apps-on-same-machine