问题
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)
回答1:
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.
}
回答2:
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.
回答3:
Allow local network loopback in "Project -> Properties -> Debugging -> Allow local network loopback".
来源:https://stackoverflow.com/questions/34428173/unable-to-access-tcp-server-inside-a-windows-universal-application