C# server client fails on connect UWP

0

I try to communicate using a simple TCP client server on UWP, I followed this link UWP socket but it looks like it doesn't works. I've added capabilities for both app to provide client & server. Even if it doesn't appear in the code, I have handled the error, which give me the following : System.Runtime.InteropServices.COMException (0x8007274C): A connection attemps has failed because the connected part has not answer after a certain amount of time or the connection has failed because host has not respond

System.Runtime.InteropServices.COMException (0x8007274C): Une tentative de connexion a échoué car le parti connecté n’a pas répondu convenablement au-delà d’une certaine durée ou une connexion établie a échoué car l’hôte de connexion n’a pas répondu.

As far I can look, it fails at the line await clientSocket.ConnectAsync(serverHost, serverPort);

At term, it should run the server on a Rasperry Pi 3 and the Client on a Windows 10 mobile (Lumia 950 XL build 14385) but until now I only terted on a surface pro 3 running Windows 10 Pro (build 14385)

Client

try
{
        StreamSocket clientSocket;

        clientSocket = new StreamSocket();
        HostName serverHost = new HostName("localhost");
        string serverPort = "5464";
        await clientSocket.ConnectAsync(serverHost, serverPort);
        Stream streamOut = clientSoket.OutputStream.AsStreamForWrite();
        StreamWriter writer = new StreamWriter(streamOut);
        string request = "test";
        await writer.WriteLineAsync(request);
        await writer.FlushAsync();
        Stream streamIn = clientSocket.InputStream.AsStreamForRead();
        StreamReader reader = new StreamReader(streamIn);
        string response = await reader.ReadLineAsync();
    }
    catch(Exception ex)
    {
        Console.WriteLine(ex.ToString());//handle
    }

Server

try
{
    serverSocket = new StreamSocketListener();
    serverSocket.ConnectionReceived += ServerSocket_ConnectionReceived;
    await serverSocket.BindServiceNameAsync("5464");
}
catch(Exception e)
{
    Console.WriteLine(e.ToString());//handle
}

private async void ServerSocket_ConnectionReceived(StreamSocketListener sender, StreamSocketListenerConnectionReceivedEventArgs args)
{
    Stream inputStream = args.Socket.InputStream.AsStreamForRead();
    StreamReader reader = new StreamReader(inputStream);
    string request = await reader.ReadLineAsync();
        Stream outputStream = args.Socket.OutputStream.AsStreamForWrite();
    StreamWriter writter = new StreamWriter(outputStream);
    await writter.WriteLineAsync("Ok");
    await writter.FlushAsync();
}
c#
client-server
uwp
asked on Stack Overflow Jul 14, 2016 by Sven Borden • edited Jul 14, 2016 by Sven Borden

1 Answer

0

After some research and thanks to Stuart Smith, It appears that two apps cannot directly connect between each other. For developement purposes, it could be allowed on a local machine, using a tool to enable network loopback. When I tried to run the client on my lumia and the server on my PC, it worked perfectly.

Here are the link used to understand this.

StackOverFlow UWP Enable loopback

Using network loopback in side-loaded Windows Store apps

answered on Stack Overflow Jul 15, 2016 by Sven Borden • edited May 23, 2017 by Community

User contributions licensed under CC BY-SA 3.0