簡體   English   中英

從UWP客戶端應用程序發送數據到服務器崩潰,錯誤為System.Runtime.InteropServices.COMException(0x8007274D)

[英]Send data from UWP client applications to server crash with error System.Runtime.InteropServices.COMException (0x8007274D)

我想創建一個服務器,以便HoloLens,UWP應用程序可以連接到它並向其發送數據。

因此,為了創建服務器,我在Visual Studio中創建了一個控制台應用程序,並按照此處的示例操作

從客戶端,UWP應用程序,我創建了以下類:

using UnityEngine;
#if !UNITY_EDITOR && UNITY_METRO
using System;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using Windows.Networking.Sockets;
using Windows.Storage.Streams;
using Windows.Networking;
using Windows.Foundation;
#endif

public class SendSocketStreamClient {
    // The port number for the remote device.  
    private int port;
    private string serverIP;

    public SendSocketStreamClient(int portNum, string ip){
        port = portNum;
        serverIP = ip;
    }

#if !UNITY_EDITOR && UNITY_METRO
    private StreamSocket networkConnection;

    // The response from the remote device.  
    private static String response = String.Empty;

    public void StartClient()
    {
        // Setup a connection to the server.
        HostName networkHost = new HostName(serverIP);
        networkConnection = new StreamSocket();

        IAsyncAction outstandingAction = networkConnection.ConnectAsync(networkHost, port.ToString());
        AsyncActionCompletedHandler aach = new AsyncActionCompletedHandler(NetworkConnectedHandler);
        outstandingAction.Completed = aach;
    }

    public void NetworkConnectedHandler(IAsyncAction asyncInfo, AsyncStatus status)
    {
        // Status completed is successful.
        if (status == AsyncStatus.Completed)
        {
            DataWriter networkDataWriter;

            // Since we are connected, we can send the data we set aside when establishing the connection.
            using (networkDataWriter = new DataWriter(networkConnection.OutputStream))
            {

                networkDataWriter.WriteBytes(Encoding.ASCII.GetBytes("Sending Trial 1"));

                // Again, this is an async operation, so we'll set a callback.
                DataWriterStoreOperation dswo = networkDataWriter.StoreAsync();
                dswo.Completed = new AsyncOperationCompletedHandler<uint>(DataSentHandler);
            }
        }
        else
        {
            // TODO resend
            Debug.Log("Failed to establish connection. Error Code: " + asyncInfo.ErrorCode);
            networkConnection.Dispose();
        }
    }

    public void DataSentHandler(IAsyncOperation<uint> operation, AsyncStatus status)
    {
        if (status == AsyncStatus.Error)
        {
            // didn't send, so requeue
            Debug.Log("Error while sending " + operation.ErrorCode);
        }
        // Always disconnect here since we will reconnect when sending the next data.  
        networkConnection.Dispose();
    }
#endif
}

然后我在C#腳本中調用此類:

#if !UNITY_EDITOR && UNITY_METRO
        SendSocketStreamClient newClient = new SendSocketStreamClient(ConnectionPort, ServerIP.Trim());
        newClient.StartClient();
#endif

但我總是得到以下錯誤。

無法建立連接。 錯誤代碼:System.Runtime.InteropServices.COMException(0x8007274D):無法建立連接,因為目標計算機主動拒絕它。

知道我做錯了什么或如何將數據從HoloLens發送到服務器? 服務器有問題嗎?

- - -編輯 - -

無法建立連接。 錯誤代碼:System.Runtime.InteropServices.COMException(0x8007274C):連接嘗試失敗,因為連接方在一段時間后沒有正確響應,或者由於連接主機無法響應而建立連接失敗。

當我將serverIP設置為我的機器的IP地址而不是127.0.0.1時,錯誤更改為上述錯誤。 所以我認為給它正確的IP地址解決了這個錯誤,但現在它沒有連接到服務器。

這是否意味着我創建服務器的方式不對?

正如上面的評論中所說的那樣我發現在我使用的服務器中

 IPAddress ipAddress = IPAddress.Loopback;

代替

IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());  
IPAddress ipAddress = ipHostInfo.AddressList[0];

更改解決了我的問題,我能夠從HoloLens連接到服務器並接收數據。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM