C#服务器客户端在连接UWP时失败 [英] C# server client fails on connect UWP
问题描述
我尝试使用UWP上的简单TCP客户端服务器进行通信,我点击了此链接
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):无法永久性地建立起汽车的部分连接性,而在可疑的汽车上将无法确定的连接性Hôtede connexion n'a pasrépondu.
据我所知,它在行 await clientSocket.ConnectAsync(serverHost,serverPort);
从一开始,它应该在Rasperry Pi 3上运行服务器,并在Windows 10移动版(Lumia 950 XL内部版本14385)上运行客户端,但是直到现在我只在运行Windows 10 Pro(内部版本14385)的Surface Pro 3上终止运行.
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)
客户
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
}
服务器
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();
}
推荐答案
经过一些研究并感谢Stuart Smith,看来两个应用程序无法直接相互连接.出于开发目的,可以使用启用网络环回的工具在本地计算机上允许它.当我尝试在lumia上运行客户端并在PC上运行服务器时,它运行良好.
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.
这篇关于C#服务器客户端在连接UWP时失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!