我编写了一些代码来将数据从外部服务器传输到 Hololens。我能够将 Hololens 连接到服务器。但是我在将数据从服务器发送到 Hololens 时遇到了问题。每当我调用ReadData
函数时,它甚至都没有连接(它打印未连接)。
我对c#
团结很陌生,还无法解决这个问题。我正在使用StreamSocket
和 DataReader 类分别连接和读取数据。函数Connect()
在方法中与服务器连接,start()
然后我ReadData
在方法中调用函数以update()
在每一帧从服务器获取数据。我附上我的代码文件。你能帮我解决我的问题提前谢谢。
using System.Collections;
using System.Collections.Generic;
using System.Net.Sockets;
using System.Threading;
using System.Text;
using System.Net;
using System;
using UnityEngine;
#if !UNITY_EDITOR && UNITY_METRO
using Windows.Networking.Sockets;
using Windows.Storage.Streams;
using Windows.Networking;
using Windows.Foundation;
#endif
public class TCPclientRead : MonoBehaviour
{
public string ServerIP = "10.1.2.35";
[Tooltip("The connection port on the machine to use.")]
public int ConnectionPort = 11000;
private string data = "connected" ;
public TextMesh mesh;
private bool connected = false;
#if !UNITY_EDITOR && UNITY_METRO
private StreamSocket networkConnection;
/// <summary>
/// Temporary buffer for the data we are recieving.
/// </summary>
//private byte[] dataBuffer;
public void Connect( )
{
// Setup a connection to the server.
HostName networkHost = new HostName(ServerIP.Trim());
//HostName networkHost = new HostName( IPAddress.Any.ToString());
networkConnection = new StreamSocket();
// Connections are asynchronous.
// !!! NOTE These do not arrive on the main Unity Thread. Most Unity operations will throw in the callback !!!
IAsyncAction outstandingAction = networkConnection.ConnectAsync(networkHost, ConnectionPort.ToString());
AsyncActionCompletedHandler aach = new AsyncActionCompletedHandler(NetworkConnectedHandler);
outstandingAction.Completed = aach;
}
public void NetworkConnectedHandler(IAsyncAction asyncInfo, AsyncStatus status)
{
if (status == AsyncStatus.Completed)
{
connected = true;
// Here Just display connected
}
else
{
connected = false;
Debug.Log("Failed to establish connection. Error Code: " + asyncInfo.ErrorCode);
// In the failure case we'll requeue the data and wait before trying again.
networkConnection.Dispose();
}
}
public void ReadData()
{
if(connected)
{
DataReader reader = new DataReader(networkConnection.InputStream);
reader.InputStreamOptions = InputStreamOptions.Partial;
reader.LoadAsync(512);
data = reader.ReadString(512);
}
}
private void Start()
{
mesh = gameObject.GetComponent<TextMesh>();
Connect();
}
private void Update()
{
if (connected)
{
mesh.text = data;
}
else
mesh.text = "Not Connected";
ReadData();
}
#endif
}
编辑: 1.我怀疑ReadData()
需要异步调用因此我更新了代码,但即使现在它也无法正常工作。2. 我正在使用 Unity Editor,并且我已经启用了所需的设置,并且我能够连接到服务器。只是我无法传输数据。3.我正在使用netcat创建服务器。
我更新的代码
delegate void DelegateMethod();
public async void ReadData()
{
if(connected)
{
DataReader reader = new DataReader(networkConnection.InputStream);
reader.InputStreamOptions = InputStreamOptions.Partial;
await reader.LoadAsync(512);
data = reader.ReadString(512);
}
}
public void AsynCall()
{
DelegateMethod dlgt = new DelegateMethod(this.ReadData);
IAsyncResult ar = dlgt.BeginInvoke(null, null);
dlgt.EndInvoke(ar);
}
private void Start()
{
mesh = gameObject.GetComponent<TextMesh>();
Connect();
}
private void Update()
{
if (connected)
{
if(String.IsNullOrEmpty(data))
data = "Improper";
AsynCall();
mesh.text = data;
}
else
mesh.text = "Not Connected";
}