将数据从 UWP 客户端应用程序发送到服务器崩溃并出现错误 System.Runtime.InteropServices.COMException (0x8007274D)

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

我想创建一个服务器,以便 UWP 应用程序 HoloLens 可以连接到它并向它发送数据。

所以为了创建服务器,我在 Visual Studio 中创建了一个控制台应用程序并按照示例 here

从客户端,UWP 应用程序,我创建了以下 class:

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# 脚本中调用这个 class:

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

但我总是得到下面的错误。

Failed to establish connection. Error Code: System.Runtime.InteropServices.COMException (0x8007274D): No connection could be made because the target machine actively refused it.

知道我做错了什么或如何将数据从 HoloLens 发送到服务器吗?服务器有问题吗?

-----编辑----

Failed to establish connection. Error Code: System.Runtime.InteropServices.COMException (0x8007274C): A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.

当我将serverIP设置为我机器的IP地址而不是127.0.0.1时,错误变成了上面的错误。所以我认为给它正确的 IP 地址解决了这个错误,但现在它没有连接到服务器。

是不是我创建服务器的方式不对?

正如上面的评论和 here 我发现在我使用的服务器中

 IPAddress ipAddress = IPAddress.Loopback;

而不是

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

更改解决了我的问题,我能够从 HoloLens 连接到服务器并接收数据。