Connecting to TCP Server running in UWP app

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'.

c#
tcp
uwp
asked on Stack Overflow Jun 30, 2020 by Hannes • edited Jul 1, 2020 by Hannes

1 Answer

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:

answered on Stack Overflow Jun 30, 2020 by Peter Torr - MSFT

User contributions licensed under CC BY-SA 3.0