1

我想创建一个服务器,以便 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地址解决了这个错误,但现在它没有连接到服务器。

这是否意味着我创建服务器的方式不对?

4

1 回答 1

0

正如上面的评论中所说,在这里我发现在我使用的服务器中

 IPAddress ipAddress = IPAddress.Loopback;

代替

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

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

于 2017-05-23T14:01:42.940 回答