What I am wanting to do is create a SocketStreamListener and connect to it (on localhost). Then connect to it and send it a message. Very simple stuff and it's all done here in an official demo but I want to understand it and use this logic in my own application.
The Problem
I have created a new Windows Metro C# application project and have this code to create a listener on my MainPage:
private void Button_Click(object sender, RoutedEventArgs e)
{
StreamSocketListener listener = new StreamSocketListener();
greetingOutput.Text = "Hello, " + nameInput.Text + "!";
}
but I get this error:
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.
The same code works in the official demo though.
What am I missing? What am I doing wrong?
You need to configure your application to require one or both of the necessary capabilities depending on your needs:
internetClientServer
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.
privateNetworkClientServer
A home or work network – the app can send information to or from your computer and other computers on the same network.
(From the documentation at http://msdn.microsoft.com/en-us/library/windows/apps/br211423.aspx)
Also see this article for more information on how capabilities works: 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.
You need to declare which capabilities your app requires (and therefore has access to) in your package manifest. Here's a step by step guide on how to do that: http://msdn.microsoft.com/en-us/library/windows/apps/br211477.aspx
You can use the Manifest Designer in Visual Studio to edit these capabilities.
Just locate and open the file in your solution named package.appxmanifest
and the Manifest Designer should open.
Select the capabilities tab and the network related capabilities your app requires and you should be good to go.
Link to the documentation about the App Manifest Designer: http://msdn.microsoft.com/en-us/library/windows/apps/br230259(v=vs.110).aspx
Regarding the last paragraph
If there is a handler for this exception, the program may be safely continued.
It is simply saying that you may wrap your code using the StreamSocketListener
in a try-catch block. This is a good thing if you want to handle the missing capabilities gracefully inside your application:
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.
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With