UWP(通用 Windows 平台)上的套接字通信 [英] Socket Communication on UWP (Universal Windows Platform)
问题描述
我想要实现的是基于 UWP 的程序,而其他 C# 程序正在尝试与一台 PC(Windows 10 专业版)上的 Socket 通信.
我尝试使用 DatagramSocket 类制作 UDP 通信模块,使用 StreamSocket 类制作 TCP 通信模块 (
我写了一个演示,其中包含 UDP 和 TCP 的服务器端和客户端.您可以在
虽然这可行,但我建议您除了开发目的之外不要使用.
What I want to implement is UWP-based programs and other C# programs are trying to communicate with Socket on one PC (Windows 10 Pro).
I have tried to make UDP communication module using DatagramSocket class and TCP communication module using StreamSocket class (https://msdn.microsoft.com/ko-kr/windows/uwp/networking/sockets) and it seems that module is not working properly.
In the Windows UWP example program and some developer forums, I saw a statement that it is impossible that socket communication between other programs on the same device.
I am wondering if it was the right I understood.
Also, if it is right, Would you recommend other ways to solve this problem?
I saw a statement that it is impossible that socket communication between other programs on the same device
It is right. According to the note of DatagramSocket official sample:
Network communications using an IP loopback address cannot normally be used for interprocess communication between a Universal Windows Platform (UWP) app and a different process (a different UWP app or a desktop app) because this is restricted by network isolation.
We cannot communicate a uwp app with other apps in a same machine.
Also, if it is right, Would you recommend other ways to solve this problem?
By testing on my side, if you use a c# console project as server and a uwp app as client, they can communicate successfully both with StreamSocket
and DatagGramSocket
. But following special steps need to pay attention to let it work.
Please ensure the Internet(Client&Server) and Private Internet(Client&Server) capabilities are enabled.
Run the server side as administrator.
Allow the network access promoted by windows when you run the server.
I wrote a demo which contains server side and client side of UDP and TCP. You can download here for testing.
And the results.
Although this can work, I recommend you to not use except for developing purpose.
这篇关于UWP(通用 Windows 平台)上的套接字通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!