简体   繁体   English

C#服务器客户端在连接UWP时失败

[英]C# server client fails on connect UWP

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. 我尝试在UWP上使用简单的TCP客户端服务器进行通信,我遵循了该链接UWP套接字,但看起来不起作用。 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): 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); 据我所知,它在await clientSocket.ConnectAsync(serverHost, serverPort);的行上失败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) 从一开始,它应该在Rasperry Pi 3上运行服务器,并在Windows 10移动版(Lumia 950 XL版本14385)上运行客户端,但是直到现在,我只在运行Windows 10 Pro(版本14385)的Surface Pro 3上终止运行。

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();
}

After some research and thanks to Stuart Smith, It appears that two apps cannot directly connect between each other. 经过一些研究并感谢Stuart Smith,看来两个应用程序无法直接相互连接。 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. 当我尝试在lumia上运行客户端并在PC上运行服务器时,它运行良好。

Here are the link used to understand this. 以下是用于了解此内容的链接。

StackOverFlow UWP Enable loopback StackOverFlow UWP启用环回

Using network loopback in side-loaded Windows Store apps 在侧面加载的Windows Store应用中使用网络环回

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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