2017-01-11 118 views
0

我寫了一些代碼將數據從外部服務器傳輸到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"; 

    } 
+0

尋求調試幫助的問題(「**爲什麼不是這個代碼工作?**」)必須包含所需的行爲,特定的問題或錯誤以及在問題本身中重現**的最短代碼** 。沒有**明確問題陳述**的問題對其他讀者無用。請參閱:[如何創建最小,完整和可驗證示例](http://stackoverflow.com/help/mcve)。 –

+0

我想你還沒有閱讀完整的文章,我提到了所需的行爲(傳輸數據),我的問題和相關的代碼。我會歡迎任何其他建議來改善帖子。 –

+0

對'Connect'的調用完成/是否觸發了回調?文檔(https://msdn.microsoft.com/en-us/library/windows/apps/hh701509)建議'ConnectAsync'應爲第二個參數取一個服務名稱,而不是端口。 –

回答

1

我懷疑是你沒有啓用UWP能力「InternetClient」,這將阻止它實際連接到遠程服務器。您沒有提及您使用的工具鏈,但是如果您處於Unity,請在「構建設置」 - >「發佈設置」 - >「Windows應用商店」 - >「發佈設置」 - >「功能」下進行檢查。如果您在Visual Studio中工作,則可以在項目屬性中進行調整。

+0

我已啓用這些屬性,這就是爲什麼我能夠連接到Hololens。我的問題是每當我調用'ReadData'函數它說沒有連接,否則它說連接。 –