2016-07-14 99 views
0

我嘗試在UWP上使用簡單的TCP客戶端服務器進行通信,我沿着此鏈接UWP socket但它看起來不起作用。我已經添加了兩個應用程序的功能,以提供客戶端&服務器。即使沒有在代碼中出現,我已經處理錯誤,這給我下面的: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 respondC#服務器客戶端連接失敗UWP

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

在它應該在Rasperry Pi 3上運行服務器,在Windows 10移動版上運行客戶端(Lumia 950 XL build 14385),但到目前爲止,我只能在運行Windows 10 Pro的Surface Pro 3上運行(構建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(); 
} 
+0

什麼UWP器件系列的測試你的代碼?桌面,手機,皮卡等我遇到同樣的問題時,測試恢復。代碼在Pi上工作,但我無法在我的臺式機上運行。 –

+0

稍後,服務器將在Pi3上,但我試圖在Windows 10上運行14385 –

+1

我相信你已經嘗試過了......但是https://msdn.microsoft.com/zh-cn/library/windows/應用程序/ Hh780593.aspx –

回答