c# - TcpListener 不适用于 UWP 应用程序

标签 c# windows uwp windows-10-universal

我正在创建一个非常简单的客户端服务器 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 经典”窗体应用程序中完美运行。但是,在我的 UWP 应用程序中,Pending 始终为“false”。它拒绝查看传入连接。我在客户端和服务器的同一台笔记本电脑上,防火墙已关闭。同样,它适用于我的表单应用程序,因此它不适用于环境。我在功能部分允许了客户端/服务器选项,但仍然没有成功。

使用 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 .

关于c# - TcpListener 不适用于 UWP 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50311627/

相关文章:

c# - 将 TimeSpan 绑定(bind)到 DataGridView 列

c# - 与 Windows 7 相比,为什么 System.Drawing.Image.GetPropertyItem 在 Windows XP/2003 上的行为不同

windows - 创建 QEmu guest 操作系统的 WinDbg 可读转储

c - 为 Unix 和 Windows 编写应用程序

c# - 是否可以在 winrt 中创建自定义转换? (如 ContentThemeTransition)

c# - 线程 MVC 6 .net 5

windows - 有没有办法为 Mac/Windows 制作双重可执行文件?

c# - 以编程方式在 Windows 10 中切换焦点辅助模式

xaml - 复选框数据绑定(bind)/最小宽度问题

c# - UWP 无法通过 FolderPicker 从硬盘访问文件夹