C# 将数据从UWP客户端应用程序发送到服务器崩溃,错误为System.Runtime.InteropServices.COMException(0x8007274D)
我想创建一个服务器,以便UWP应用程序HoloLens能够连接到它并向它发送数据 为了创建服务器,我在VisualStudio中创建了一个控制台应用程序,并遵循下面的示例 在客户端UWP应用程序中,我创建了以下类:C# 将数据从UWP客户端应用程序发送到服务器崩溃,错误为System.Runtime.InteropServices.COMException(0x8007274D),c#,server,uwp,stream-socket-client,windows-networking,C#,Server,Uwp,Stream Socket Client,Windows Networking,我想创建一个服务器,以便UWP应用程序HoloLens能够连接到它并向它发送数据 为了创建服务器,我在VisualStudio中创建了一个控制台应用程序,并遵循下面的示例 在客户端UWP应用程序中,我创建了以下类: using UnityEngine; #if !UNITY_EDITOR && UNITY_METRO using System; using System.Text; using System.Collections; using System.Collection
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
}
但我总是得到下面的错误
无法建立连接。错误代码: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连接到服务器并接收数据。正如上面的评论所述,以及我在使用的服务器中发现的那样
IPAddress ipAddress = IPAddress.Loopback;
而不是
IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
IPAddress ipAddress = ipHostInfo.AddressList[0];
更改解决了我的问题,我能够从HoloLens连接到服务器并接收数据。localhost(或127.0.0.1)的问题是由于所有UWP应用程序都有以下限制而发生的:。可能会帮助您调试这个(它还有一个内置的工具,可以启用环回)谢谢您的链接。我已经发现了问题。=)localhost(或127.0.0.1)出现问题是因为所有UWP应用程序都有以下限制:。可能会帮助您调试这个(它还有一个内置的工具,可以启用环回)谢谢您的链接。我已经发现了问题。=)