6

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.

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

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

Thanks!

Joao

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.

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!

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.

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)

JoaoSantos
  • 103
  • 1
  • 6

4 Answers4

1

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
Alon Catz
  • 2,417
  • 1
  • 19
  • 23
0

I'm not sure if telnet would even work for connecting to a StreamSocketListener. I've never tried that. Try using the below and let me know if it works. I changed slightly the call to bind the listener since this is how I have it working in my setup. Also, keep in mind that local network loopback like this will work in debug, but it won't work for published store apps.

If it works, you should get a message dialog showing that the ConnectionReceived event fired. :) Hope that helps!

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

var ss = new StreamSocket();
await ss.ConnectAsync(new HostName("127.0.0.1"), 8080);

private async void ProcessRequestAsync(StreamSocket e)
{
    await new MessageDialog("Connection received!", "New Connection").ShowAsync();
    // The rest of your code goes here.
}
Jared
  • 764
  • 7
  • 17
  • Thanks Jared, however, my problem is not connecting to the listener from within the same application, This I know it works without any problem. The issue here, is connecting from another application (Client/Server). – JoaoSantos Dec 23 '15 at 19:29
  • Regarding the Telnet, I know it works because I have tested the exact same code from a simple console application (not Windows universal) – JoaoSantos Dec 23 '15 at 19:29
  • Hi there, Joao. I've tested trying to connect to a UWP StreamSocketListener with a WPF app using sockets, and it doesn't work. The same server accepts connections from another computer using a UWP client, though. That leads me to believe that you must be using the UWP socket APIs in order to connect to the UWP server. – Jared Dec 23 '15 at 23:26
  • Try connecting to your server with a UWP StreamSocket client and let me know if it works. I have a working UWP server that I host on an Azure VM, and I can connect to it from any Windows 10 device. Also, remember to open the port in your firewall and configure port forwarding if behind a different router. :) – Jared Dec 23 '15 at 23:32
  • So, you confirm what I'm claiming. Under the same computer (at least), there is no way to connect to the UWP server using a non UWP application? – JoaoSantos Dec 23 '15 at 23:32
  • Sorry, I didn't see your edit until now. No, I don't think there is. I've tried too and wasn't able to find a way. I even tried referencing the WinRT socket API's in a WPF desktop app. If you do discover something, I'd be very interested to hear about it! :) – Jared Dec 23 '15 at 23:35
  • 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! – JoaoSantos Dec 23 '15 at 23:35
0

Have you tried to enable Private network capability on your UWP? I use it for a similar project.

I think that you need this capability if you want to manage a local network connection.

ganchito55
  • 3,559
  • 4
  • 25
  • 46
  • Yes, I already did that (Edit #4). I allowed me to access the application from another device within my network, but still no luck with localhost. Also tried adding an LoopbackExempt for the application, but with the same result. – JoaoSantos Dec 24 '15 at 17:02
0

Allow local network loopback in "Project -> Properties -> Debugging -> Allow local network loopback".