繁体   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