本文介绍了无法访问 Windows 通用应用程序内的 TCP 服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Windows 通用应用程序,它应该充当 TCP 服务器.

I have a Windows Universal Application that is supposed to act as a TCP server.

m_Listener = new StreamSocketListener();
m_Listener.ConnectionReceived += (s, e) => ProcessRequestAsync(e.Socket);
var bind = m_Listener.BindServiceNameAsync("8080").Wait();

在启动应用程序之前,端口没有绑定到任何东西:

Before starting the application, the port is not bind to anything:

C:\Users\jsant>netstat -a | grep 8080
^C (Cancelled after some seconds because no reasults were found)

然后,当我启动应用程序时:

Then, when I start the application:

C:\Users\jsant>netstat -a | grep 8080
  TCP    0.0.0.0:8080           Laptop:0       LISTENING

所以套接字正在我的电脑上监听!试图以某种方式访问​​它,导致错误

So the socket is listening on my computer!Trying to access it somehow, results in an error

C:\Users\jsant>telnet 127.0.0.1 8080
Connecting To 127.0.0.1...Could not open connection to the host, on port 8080: Connect failed
C:\Users\jsant>telnet 192.168.1.167 8080
Connecting To 192.168.1.167...Could not open connection to the host, on port 8080: Connect failed
C:\Users\jsant>telnet Laptop 8080
Connecting To Laptop...Could not open connection to the host, on port 8080: Connect failed
C:\Users\jsant>telnet localhost 8080
Connecting To localhost...Could not open connection to the host, on port 8080: Connect failed

我已激活所有功能以将其作为可能的原因删除.

I have activated all the capabilities to remove that as a possible reason.

在控制台应用程序上运行的相同代码运行良好.

The same code running on a console application is working just fine.

此外,运行 MS 示例https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/DatagramSocket 有相同的结果.在应用程序内部,套接字是可见的,在外部,则不可见!

Also, running the MS example fromhttps://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/DatagramSocket had the same result.Inside the application the socket was visible, outside, not!

在 Windows 通用应用程序中运行服务器是否有任何限制,或者我是否遗漏了什么?

Is there any limitation of running a server in a Windows Universal Application, or am I missing something?

谢谢!

若昂

编辑 我想我还不够清楚.我的问题是从外部应用程序访问 TCP 服务器.应用程序可以是 telnet、Web 浏览器等.服务器位于 Windows 通用应用程序内.

Edit I think I wasn't clear enough. My problem is accessing the TCP Server from an external application. The application can either be a telnet, web browser, etc. The server is located inside the Windows Universal App.

编辑 #2
我花了最后一个小时来改变这个例子:https://ms-iot.github.io/content/en-US/win10/samples/BlinkyWebServer.htm 并在我的 Raspberry Pi 2 中运行它我能够从我的计算机访问网络服务器.在我的计算机上运行相同的应用程序我无法连接.这真的很奇怪!

Edit #2
I've spent the last hour changing this example : https://ms-iot.github.io/content/en-US/win10/samples/BlinkyWebServer.htm and running it in my Raspberry Pi 2 I was able to access the webserver from my computer. Running the same application in my computer I was unable to connect. This is really strange!

编辑 #3 我已经更改了我的应用程序,只保留了 Internet(客户端和服务器)功能并且没有声明.在 Raspberry Pi 上运行完美无缺,在我的本地计算机上,仍然没有运气(本地或远程).是的,防火墙已禁用:).根据 Jared 的评论,他确认了他的计算机存在同样的问题,在我看来,这似乎是一个错误.我会继续调查.

Edit #3 I've changed my application, left only the Internet (Client & Server) capability and no declarations. Running in the Raspberry Pi works flawlessly, in my local computer, still no luck (local or remote). And yes, the firewall is disabled :). Based on the Jared's comments, where he confirms the same problem with his computer, this seems to me to be a bug. I'll continue to investigate.

编辑 #4 在了解命令CheckNetIsolation"后,添加功能PrivateNetworkClientServer"允许我从外部设备访问.仍然无法从本地计算机访问它(即使添加了 LoopbackExempt 规则)

Edit #4 after learning about the command "CheckNetIsolation", adding the capability "PrivateNetworkClientServer" allowed me to have access from an external device. Still, unable to access it from the local computer (even with a LoopbackExempt rule added)

推荐答案

我遇到了同样的问题.这篇文章很好地描述了行为.简而言之,当您的 UWP 应用侦听 TCP 套接字并且您尝试从非 UWP 应用连接时,您必须让 CheckNetIsolation.exe 连续运行 -是这样的标志:

I had the same problem. This article gives a good description of the behaviour. In a nutshell, when your UWP app listens on a TCP socket and you try to connect from a non UWP app, you have to have CheckNetIsolation.exe running continuously with the -is flag like this:

CheckNetIsolation.exe LoopbackExempt -is -n=b6823a8d-d94d-4de4-b7db-4e3567ca11c8_t0fze8msm45va

这篇关于无法访问 Windows 通用应用程序内的 TCP 服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 07:58
查看更多