[英]C# server client fails on connect UWP
我嘗試在UWP上使用簡單的TCP客戶端服務器進行通信,我遵循了該鏈接UWP套接字,但看起來不起作用。 我為兩個應用程序都添加了功能,以提供客戶端和服務器。 即使它沒有出現在代碼中,我也已經處理了錯誤,該錯誤為我提供了以下內容: 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.
據我所知,它在await clientSocket.ConnectAsync(serverHost, serverPort);
的行上失敗await clientSocket.ConnectAsync(serverHost, serverPort);
從一開始,它應該在Rasperry Pi 3上運行服務器,並在Windows 10移動版(Lumia 950 XL版本14385)上運行客戶端,但是直到現在,我只在運行Windows 10 Pro(版本14385)的Surface Pro 3上終止運行。
客戶
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上運行服務器時,它運行良好。
以下是用於了解此內容的鏈接。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.