gpt4 book ai didi

c# - StreamSocketListener 访问被拒绝

转载 作者:行者123 更新时间:2023-11-30 20:05:15 25 4
gpt4 key购买 nike

我想要做的是创建一个 SocketStreamListener 并连接到它(在本地主机上)。然后连接到它并向它发送消息。很简单的东西就搞定了here in an official demo但我想理解它并在我自己的应用程序中使用这个逻辑。

问题

我创建了一个新的 Windows Metro C# 应用程序项目,并使用以下代码在我的主页上创建一个监听器:

private void Button_Click(object sender, RoutedEventArgs e)
{
StreamSocketListener listener = new StreamSocketListener();
greetingOutput.Text = "Hello, " + nameInput.Text + "!";
}

但是我得到这个错误:

An exception of type 'System.UnauthorizedAccessException' occurred in HelloWorld.exe but was not handled in user code

WinRT information: At least one of either InternetClientServer or PrivateNetworkClientServer capabilities is required to listen for or receive traffic

Additional information: Access is denied.

If there is a handler for this exception, the program may be safely continued.

虽然在官方演示中可以使用相同的代码。

我错过了什么?我做错了什么?

最佳答案

您需要根据您的需要将您的应用程序配置为需要一项或两项必要功能:

  1. 互联网客户端服务器

    Your Internet connection, including incoming unsolicited connections from the Internet – the app can send information to or from your computer through a firewall. You do not need to declare internetClient if this capability is declared.

  2. 私有(private)网络客户端服务器

    A home or work network – the app can send information to or from your computer and other computers on the same network.

(来自 http://msdn.microsoft.com/en-us/library/windows/apps/br211423.aspx 的文档)

另请参阅本文以获取有关功能如何工作的更多信息:http://msdn.microsoft.com/en-us/library/windows/apps/hh464936.aspx

Internet and public networks

The internetClient capability provides outbound access to the Internet and public networks through the firewall. Almost all web apps use this capability. The internetClientServer capability provides inbound and outbound access to the Internet and public networks through the firewall.

Home and work networks

The privateNetworkClientServer capability provides inbound and outbound access to home and work networks through the firewall. This capability is typically used for games that communicate across the local area network (LAN), and for apps that share data across a variety of local devices. If your app specifies musicLibrary, picturesLibrary, or videosLibrary, you don't need to use this capability to access the corresponding library in a Home Group.

您需要声明您的应用需要哪些功能(因此可以访问)在您的包裹 list 中。这是有关如何执行此操作的分步指南:http://msdn.microsoft.com/en-us/library/windows/apps/br211477.aspx

您可以使用 Visual Studio 中的 list 设计器来编辑这些功能。只需在您的解决方案中找到并打开名为 package.appxmanifest 的文件, list 设计器就会打开。

App Manifest Designer

选择功能选项卡和您的应用所需的网络相关功能,您应该一切顺利。

有关 App Manifest Designer 的文档链接:http://msdn.microsoft.com/en-us/library/windows/apps/br230259(v=vs.110).aspx

关于最后一段

If there is a handler for this exception, the program may be safely continued.

这只是说您可以在 try-catch block 中使用 StreamSocketListener 包装您的代码。如果您想在应用程序中优雅地处理缺失的功能,这是一件好事:

private void Button_Click(object sender, RoutedEventArgs e)
{
try
{
StreamSocketListener listener = new StreamSocketListener();
greetingOutput.Text = "Hello, " + nameInput.Text + "!";
}
catch(UnauthorizedAccessException exc)
{
// Act on the missing capability. Log it and/or warn the user.
}
}

关于c# - StreamSocketListener 访问被拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11725170/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com