0

I am attempting to run a TCP server from my UWP App. However i cannot connect to the server from another application (which im running on the same PC). I tried using the telnet command.

This snippet works correctly if i use it in a command Line Application, in the UWP app it does execute, but never gets any connection requests.

TcpListener serverSocket = new TcpListener(System.Net.IPAddress.Parse("127.0.0.1"), 3457);
int requestCount = 0;
TcpClient clientSocket = default(TcpClient);
serverSocket.Start();
Console.WriteLine(" >> Server Started at " + serverSocket.LocalEndpoint);
while (!serverSocket.Pending()) ;      //The App will loop infinitely here never receiving any requests.
clientSocket = serverSocket.AcceptTcpClient();
Console.WriteLine(" >> Accept connection from client");
requestCount = 0;

while ((true))
{
    try
    {
        requestCount = requestCount + 1;
        NetworkStream networkStream = clientSocket.GetStream();
        byte[] bytesFrom = new byte[65536];
        networkStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize);
        string dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom);
        dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf("\0"));
        Console.WriteLine(" >> Data from client - " + dataFromClient);
        string serverResponse = "Last Message from client: " + dataFromClient;
        Byte[] sendBytes = Encoding.ASCII.GetBytes(serverResponse);
        networkStream.Write(sendBytes, 0, sendBytes.Length);
        networkStream.Flush();
        Console.WriteLine(" >> " + serverResponse);
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.ToString());
    }
}

I also tried the example here but i still could not connect. In that page it is also mentioned that i two uwp apps on the same PC cannot communicate over TCP because of network isolation. My second application is not uwp but i still disabled it with checknetisolation loopbackexempt -a -n=packagename. which did not solve the problem either. since it does work with Console i think it has to do with UWP preventing me from accessing it, however i don't now why. I disabled the firewall but that was not the problem either. If anyone knows what the issue is or has some ideas i would be grateful.

I added the following to my Application in the manifest:

<Extensions>
    <uap4:Extension xmlns:uap4="http://schemas.microsoft.com/appx/manifest/uap/windows10/4" Category="windows.loopbackAccessRules">
        <uap4:LoopbackAccessRules>
            <uap4:Rule Direction="out" PackageFamilyName="My App name"/>
        </uap4:LoopbackAccessRules>
    </uap4:Extension>
</Extensions>

if i use the package family name i get an error DEP0700: Registration of the app failed. [0x80073CF6] error 0x8000FFFF and it says while trying to register the loopback accesRules it failed because of 'Catastrophic failure'

if i use the app name however i can compile but a error messages pops up

Unable to activate Windows Store app 'XXXX_5f208x3ge840e!App'. The activation request failed with error 'The application cannot be started. Try reinstalling the application to fix the problem'.

Hannes
  • 442
  • 2
  • 10
  • You can have only one connection with the same three parameters 1) Source IP 2) Destination IP 3) Port. When you have a listener you can get multiple connections from different IP addresses (clients on different machines). If you want multiple connection from same machine you need to use a different port number. which means more than one listener. – jdweng Jun 30 '20 at 15:36
  • I just want one connection (from one program to my UWP app) (Currently im testing this with the telnet command in the console and cannot connect to the port) – Hannes Jun 30 '20 at 15:39
  • @jdweng - source port and destination port are both part of the uniqueness and usually the source ports vary enough. Browsers will often open 10s of connections to a single site if still using HTTP 1.1 and those are all to destination port 80 (or 443) – Damien_The_Unbeliever Jun 30 '20 at 15:48
  • When the client and server are on the same machine each has to use a different set of the three parameters. So normally you have Listener using for an endpoint IP.Any and the client connecting to the IP address or the machine so you have two different IPs. I do not like using loopback IP 127.0.0.1 which may not work depending on how the HOST file on machine is configured. – jdweng Jun 30 '20 at 15:52
  • @ Damien_The_Unbeliever : This is TCP and not HTTP. HTTP the machine has a service that can handle port forwarding duplicates to the proper application. TCP there is nothing on the machine to handle duplicates. – jdweng Jun 30 '20 at 15:55
  • @jdweng I tried different ip-addresses as well and they work with the console app but not with uwp – Hannes Jun 30 '20 at 15:58
  • @jdweng - it's *not* 3 parameters. It's 4. source IP, destination IP, source port, destination port. And unless people go out of there way to do odd things, they'll be letting the OS assign source ports arbitrarily. – Damien_The_Unbeliever Jun 30 '20 at 16:06
  • Try index 1 instead of index 0. I think index zero is IPV6 and index 1 is IPV4 : dataFromClient.IndexOf("\0") – jdweng Jun 30 '20 at 16:08
  • @jdweng The App will never get over the loop 'while (!serverSocket.Pending()) ;' so this is not even executed now. – Hannes Jun 30 '20 at 16:15
  • You will not get pass the line of code until the client connects, and the client will not connect until you get pass the line of code. This is called an chicken and egg problem (which came first). – jdweng Jun 30 '20 at 16:24
  • Why do i need to be passed this line to get connected? Using the same exact same code on console application works fine. It just waits for new requests to be connected and as soon as i get one i will go past that line and connect to it – Hannes Jun 30 '20 at 16:27
  • according to this it is not possible to do what i am trying to https://stackoverflow.com/questions/54525530/loopback-isolation-removal-not-working-for-uwp-app – Hannes Jun 30 '20 at 16:47
  • What format did you use for the package name? Note that this is only applicable if the *other* app is packaged; if it's not packaged then you need to use `CheckNetIsolation` instead. – Peter Torr - MSFT Jul 01 '20 at 17:31
  • ok, yes i used the package name of the app (with the server) itself but i am supposed to set the name of the target client app right? Currently i am just using the console to get a connection running so i am not sure if this applies here as well? Or what name would i use for that? I tried using the CheckNetIsolation and i can disable loopback prevention and it says 'OK' but i still cant connect. – Hannes Jul 02 '20 at 07:33

2 Answers2

0

Loopback is not supported for UWP apps by default; there is some configuration you need to do first. It is outlined in this MSDN document. In short:

Peter Torr - MSFT
  • 11,824
  • 3
  • 18
  • 51
  • 1
    Thanks for the answer, however im not sure how to us the LoopbackAccessRules. I edited my questions so maybe you can spot my mistake – Hannes Jul 01 '20 at 06:58
0

First, i need to clarify the wording

  • packaged == store/winrt/uap/uwp app
  • unpacked == everything else; your perfectly ordinary Win32/.net app (without DesktopBridge or anything special); Browsers, VS, java, python (if not installed via store), SOAPUI, ...

Second

The fundamental mechanism you are facing can be seen as a simple firewall, wich is working on app-level, not network-level. You have very limited possiblities to change the behaviour freely and no possiblities to do that permanently on your customer PCs.

Third, guidance for picking the right tool/setting

All the following refers specifically to localhost.

for connecting from Win32 client to UAP server:

  • CheckNetIsolation.exe LoopbackExempt -is -n="My App name"
    • it's only temporarily!
    • run via Administrator
    • keep the process running
    • need at least "Windows 10, version 1607"
    • this allows just incomming connections (from Win32 client to UAP Server)
      • not the other way around

for connecting from UAP client to win32 server:

  • CheckNetIsolation.exe LoopbackExempt -a -n="My App name"
    • adds the rule permanently to your computer/user settings
    • can be executed as regular user, no admin right needed
    • this allows just outgoing connections (from UAP client to Win32 Server)

for connecting from UAP client to UAP server from different Apps:

  • use the package.appxmanifest
  • might need to edit for both apps
<?xml version="1.0" encoding="utf-8"?>

<Package xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10" 
  xmlns:uap4="http://schemas.microsoft.com/appx/manifest/uap/windows10/4">

  <Applications>
    <Application Id="App" Executable="$targetnametoken$.exe" EntryPoint="MyApp.UWP.App">
      <Extensions>
        <uap4:Extension Category="windows.loopbackAccessRules" >
          <uap4:LoopbackAccessRules>
            <uap4:Rule Direction="in" PackageFamilyName="My other App name" />
            <uap4:Rule Direction="out" PackageFamilyName="My other App name" />
          </uap4:LoopbackAccessRules>
        </uap4:Extension>
      <Extensions>
    </Application>
  <Applications>
</Applications>
</Package>

Back to the Future

Now, after almost a decade, a MS manager had an insight and the chances are good, that there will be regular functionality for loopback communication via project reunion:

https://github.com/microsoft/ProjectReunion/issues/113

juwens
  • 3,729
  • 4
  • 31
  • 39
  • It's redicoulus that this is so complicated. ### and MS refused to fix that for almost a decade, because: "it's supposed to be this way" ### the docs are sometimes poor/confusing at explaining these things. # maybe because MS doesn't want you to use this "feature" ### MS also uses the same word "UWP" for a lot of different things (Store Apps, MSIX-Packaging, UI Framework, WinRT, ...) ### and on the other hand they use words like (un)packaged out of the blue on some MSDN sites – juwens Jun 07 '21 at 21:53
  • Agreed. I have a UWP app and a windows service app that both want to talk to each other. There should be a way to setup a trusted connection between the 2. Just another UWP limitation that makes working in .NET harder. – Guy Lowe Mar 28 '23 at 01:19