简体   繁体   中英

TcpListener not working on UWP app

I am creating a very simple client server UWP app. I have the following code.

private void Button_Click(object sender, RoutedEventArgs e)
{
    TcpListener server;

    server = new TcpListener(IPAddress.Any, 8081);
    server.Start();

    while (!server.Pending())
    {
    }

    Console.WriteLine("Connected");
}

This code works perfectly in a "Windows Classic" forms app. However Pending is always "false" in my UWP app. It refuses to see incoming connection. I am on the same laptop for the client and server and the firewall is turned off. Again, it works in my forms app so its not the environment. I have allowed client/server options in the capabilities section but still no luck.

Using netstat I can see that port 8081 is listening.

Any ideas what I am missing here? Thank you.

I am on the same laptop for the client and server

According to your description, the server and the client are on the same device. According to the note section of 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.

We cannot establishing a socket connection between two UWP apps running on the same machine. Not even with a loopback exemption. This is as design.

More details please reference the official sample .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM