TcpListener 不适用于 UWP 应用程序
TcpListener not working on UWP app
我正在创建一个非常简单的客户端服务器 UWP 应用程序。我有以下代码。
private void Button_Click(object sender, RoutedEventArgs e)
{
TcpListener server;
server = new TcpListener(IPAddress.Any, 8081);
server.Start();
while (!server.Pending())
{
}
Console.WriteLine("Connected");
}
此代码在 "Windows Classic" 表单应用程序中完美运行。但是,在我的 UWP 应用程序中,Pending 始终为 "false"。它拒绝查看传入连接。我在客户端和服务器的同一台笔记本电脑上,防火墙已关闭。同样,它适用于我的表单应用程序,因此它不适用于环境。我在功能部分允许 client/server 选项,但仍然没有成功。
使用 netstat 我可以看到端口 8081 正在侦听。
知道我在这里遗漏了什么吗?谢谢。
I am on the same laptop for the client and server
根据您的描述,服务端和客户端在同一台设备上。根据this documents的注释部分:
As a consequence of network isolation, Windows disallows establishing a socket connection (Sockets or WinSock) between two UWP apps running on the same machine; whether that's via the local loopback address (127.0.0.0), or by explicitly specifying the local IP address. For details about mechanisms by which UWP apps can communicate with one another, see App-to-app communication.
我们无法在同一台机器上的两个 UWP 应用程序之间建立套接字连接 运行。甚至没有环回豁免。这就是设计。
更多详情请参考official sample。
我正在创建一个非常简单的客户端服务器 UWP 应用程序。我有以下代码。
private void Button_Click(object sender, RoutedEventArgs e)
{
TcpListener server;
server = new TcpListener(IPAddress.Any, 8081);
server.Start();
while (!server.Pending())
{
}
Console.WriteLine("Connected");
}
此代码在 "Windows Classic" 表单应用程序中完美运行。但是,在我的 UWP 应用程序中,Pending 始终为 "false"。它拒绝查看传入连接。我在客户端和服务器的同一台笔记本电脑上,防火墙已关闭。同样,它适用于我的表单应用程序,因此它不适用于环境。我在功能部分允许 client/server 选项,但仍然没有成功。
使用 netstat 我可以看到端口 8081 正在侦听。
知道我在这里遗漏了什么吗?谢谢。
I am on the same laptop for the client and server
根据您的描述,服务端和客户端在同一台设备上。根据this documents的注释部分:
As a consequence of network isolation, Windows disallows establishing a socket connection (Sockets or WinSock) between two UWP apps running on the same machine; whether that's via the local loopback address (127.0.0.0), or by explicitly specifying the local IP address. For details about mechanisms by which UWP apps can communicate with one another, see App-to-app communication.
我们无法在同一台机器上的两个 UWP 应用程序之间建立套接字连接 运行。甚至没有环回豁免。这就是设计。
更多详情请参考official sample。