我寫了一些代碼將數據從外部服務器傳輸到Hololens。我能夠將Hololens連接到服務器。但是我在將數據從服務器發送到Hololens時遇到問題。每當我打電話ReadData
功能它甚至沒有連接(它打印未連接)。
我對c#
和團結相當新,但不能解決這個問題呢。 我正在使用StreamSocket
和DataReader類來分別連接和讀取數據。功能Connect()
方法start()
方法連接服務器,然後我撥打ReadData
功能update()
方法在每個幀從服務器獲取數據。我附上我的代碼文件。 你能幫我解決我的問題提前謝謝。無法從外部服務器向Hololens讀取數據
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編輯器,我已啓用所需的設置,我可以連接到服務器。只是我無法傳輸數據。 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";
}
尋求調試幫助的問題(「**爲什麼不是這個代碼工作?**」)必須包含所需的行爲,特定的問題或錯誤以及在問題本身中重現**的最短代碼** 。沒有**明確問題陳述**的問題對其他讀者無用。請參閱:[如何創建最小,完整和可驗證示例](http://stackoverflow.com/help/mcve)。 –
我想你還沒有閱讀完整的文章,我提到了所需的行爲(傳輸數據),我的問題和相關的代碼。我會歡迎任何其他建議來改善帖子。 –
對'Connect'的調用完成/是否觸發了回調?文檔(https://msdn.microsoft.com/en-us/library/windows/apps/hh701509)建議'ConnectAsync'應爲第二個參數取一個服務名稱,而不是端口。 –