将数据从 UWP 客户端应用程序发送到服务器崩溃,并显示错误 System.Runtime.InteropService



我想创建一个服务器,以便 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):一个连接 尝试失败,因为连接的一方未正确响应 一段时间后,或建立的连接失败,因为 连接的主机无法响应。

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

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

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

IPAddress ipAddress = IPAddress.Loopback;

而不是

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

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

相关内容

  • 没有找到相关文章

最新更新