Search Unity

Question Unity 2019.4.9f -- Cannot establish UDP/TCP connection with HoloLens 2

Discussion in 'AR/VR (XR) Discussion' started by avikom, Sep 9, 2020.

  1. avikom

    avikom

    Joined:
    Jun 18, 2019
    Posts:
    7
    No matter what I try, I cannot connect to the HoloLens 2. I tried UDP and TCP, both without success. I had the same issue with the HoloLens 1 which I could solve by deactivating automatic proxy setting detection. However, this does not work for HoloLens 2. When deployed, the TCPTestClient below can reach the TCPTestServer locally but I cannot reach it from another machine. The HoloLens 2 however can reach my machine without any issues though. My project has `EnterpriseAuthentication`, `InternetClient`, `InternetClientServer`, `PrivateNetworkClientServer` enabled in addition to the settings MRTK is setting when imported into a project.

    EDIT:
    I tried port 9091 but will give 8000 a try since this one is used for Holographic Remoting and thus should be reachable from remote. Update: Nope, no luck. Socket times out anyway.

    EDIT2:
    I use VS2019, my MS SDK is 10.0.18362, I checked with another Wifi Network (my phone acting as a hotspot), I can reach the HoloLens 2 via Holographic Remoting and I can also reach the device portal via Wifi.

    TCPTestClient


    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using System.Net.Sockets;
    5. using System.Text;
    6. using System.Threading;
    7. using UnityEngine;
    8.  
    9. public class TCPTestClient : MonoBehaviour
    10. {
    11.     #region private members
    12.     private TcpClient socketConnection;
    13.     private Thread clientReceiveThread;
    14.     #endregion
    15.     // Use this for initialization
    16.     void Start()
    17.     {
    18.         ConnectToTcpServer();
    19.         Invoke("SendMessage", 5.0f);
    20.     }
    21.     // Update is called once per frame
    22.     /// <summary>
    23.     /// Setup socket connection.
    24.     /// </summary>
    25.     private void ConnectToTcpServer()
    26.     {
    27.         try
    28.         {
    29.             clientReceiveThread = new Thread(new ThreadStart(ListenForData));
    30.             clientReceiveThread.IsBackground = true;
    31.             clientReceiveThread.Start();
    32.         }
    33.         catch (Exception e)
    34.         {
    35.             Debug.Log("On client connect exception " + e);
    36.         }
    37.     }
    38.     /// <summary>
    39.     /// Runs in background clientReceiveThread; Listens for incomming data.
    40.     /// </summary>
    41.     private void ListenForData()
    42.     {
    43.         try
    44.         {
    45.             socketConnection = new TcpClient("localhost", 9091);
    46.             Byte[] bytes = new Byte[1024];
    47.             while (true)
    48.             {
    49.                 // Get a stream object for reading            
    50.                 using (NetworkStream stream = socketConnection.GetStream())
    51.                 {
    52.                     int length;
    53.                     // Read incomming stream into byte arrary.                
    54.                     while ((length = stream.Read(bytes, 0, bytes.Length)) != 0)
    55.                     {
    56.                         var incommingData = new byte[length];
    57.                         Array.Copy(bytes, 0, incommingData, 0, length);
    58.                         // Convert byte array to string message.                    
    59.                         string serverMessage = Encoding.ASCII.GetString(incommingData);
    60.                         Debug.Log("server message received as: " + serverMessage);
    61.                     }
    62.                 }
    63.             }
    64.         }
    65.         catch (SocketException socketException)
    66.         {
    67.             Debug.Log("Socket exception: " + socketException);
    68.         }
    69.     }
    70.     /// <summary>
    71.     /// Send message to server using socket connection.
    72.     /// </summary>
    73.     private void SendMessage()
    74.     {
    75.         if (socketConnection == null)
    76.         {
    77.             return;
    78.         }
    79.         try
    80.         {
    81.             // Get a stream object for writing.        
    82.             NetworkStream stream = socketConnection.GetStream();
    83.             if (stream.CanWrite)
    84.             {
    85.                 string clientMessage = "This is a message from one of your clients.";
    86.                 // Convert string message to byte array.            
    87.                 byte[] clientMessageAsByteArray = Encoding.ASCII.GetBytes(clientMessage);
    88.                 // Write byte array to socketConnection stream.            
    89.                 stream.Write(clientMessageAsByteArray, 0, clientMessageAsByteArray.Length);
    90.                 Debug.Log("Client sent his message - should be received by server");
    91.             }
    92.         }
    93.         catch (SocketException socketException)
    94.         {
    95.             Debug.Log("Socket exception: " + socketException);
    96.         }
    97.     }
    98. }
    TCPTestServer

    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using System.Net;
    5. using System.Net.Sockets;
    6. using System.Text;
    7. using System.Threading;
    8. using UnityEngine;
    9.  
    10. public class TCPTestServer : MonoBehaviour
    11. {
    12.     #region private members
    13.     /// <summary>
    14.     /// TCPListener to listen for incomming TCP connection
    15.     /// requests.
    16.     /// </summary>
    17.     private TcpListener tcpListener;
    18.     /// <summary>
    19.     /// Background thread for TcpServer workload.
    20.     /// </summary>
    21.     private Thread tcpListenerThread;
    22.     /// <summary>
    23.     /// Create handle to connected tcp client.
    24.     /// </summary>
    25.     private TcpClient connectedTcpClient;
    26.     #endregion
    27.  
    28.     // Use this for initialization
    29.     void Start()
    30.     {
    31.         // Start TcpServer background thread    
    32.         tcpListenerThread = new Thread(new ThreadStart(ListenForIncommingRequests));
    33.         tcpListenerThread.IsBackground = true;
    34.         tcpListenerThread.Start();
    35.     }
    36.  
    37.     /// <summary>
    38.     /// Runs in background TcpServerThread; Handles incomming TcpClient requests
    39.     /// </summary>
    40.     private void ListenForIncommingRequests()
    41.     {
    42.         try
    43.         {
    44.             // Create listener on localhost port 8052.        
    45.             tcpListener = new TcpListener(IPAddress.Parse("0.0.0.0"), 9091);
    46.             tcpListener.Start();
    47.             Debug.Log("Server is listening");
    48.             Byte[] bytes = new Byte[1024];
    49.             while (true)
    50.             {
    51.                 using (connectedTcpClient = tcpListener.AcceptTcpClient())
    52.                 {
    53.                     // Get a stream object for reading                
    54.                     using (NetworkStream stream = connectedTcpClient.GetStream())
    55.                     {
    56.                         int length;
    57.                         // Read incomming stream into byte arrary.                    
    58.                         while ((length = stream.Read(bytes, 0, bytes.Length)) != 0)
    59.                         {
    60.                             var incommingData = new byte[length];
    61.                             Array.Copy(bytes, 0, incommingData, 0, length);
    62.                             // Convert byte array to string message.                        
    63.                             string clientMessage = Encoding.ASCII.GetString(incommingData);
    64.                             Debug.Log("client message received as: " + clientMessage);
    65.                             SendMessage();
    66.                         }
    67.                     }
    68.                 }
    69.             }
    70.         }
    71.         catch (SocketException socketException)
    72.         {
    73.             Debug.Log("SocketException " + socketException.ToString());
    74.         }
    75.     }
    76.     /// <summary>
    77.     /// Send message to client using socket connection.
    78.     /// </summary>
    79.     private void SendMessage()
    80.     {
    81.         if (connectedTcpClient == null)
    82.         {
    83.             return;
    84.         }
    85.  
    86.         try
    87.         {
    88.             // Get a stream object for writing.        
    89.             NetworkStream stream = connectedTcpClient.GetStream();
    90.             if (stream.CanWrite)
    91.             {
    92.                 string serverMessage = "This is a message from your server.";
    93.                 // Convert string message to byte array.            
    94.                 byte[] serverMessageAsByteArray = Encoding.ASCII.GetBytes(serverMessage);
    95.                 // Write byte array to socketConnection stream.          
    96.                 stream.Write(serverMessageAsByteArray, 0, serverMessageAsByteArray.Length);
    97.                 Debug.Log("Server sent his message - should be received by client");
    98.             }
    99.         }
    100.         catch (SocketException socketException)
    101.         {
    102.             Debug.Log("Socket exception: " + socketException);
    103.         }
    104.     }
    105. }
     
    Last edited: Sep 10, 2020
  2. avikom

    avikom

    Joined:
    Jun 18, 2019
    Posts:
    7
    So, this code above does not work very well because the server isn't printing received messages probably related to threading. However, I was only interested in whether the client can establish a connection. I don't know what made it work: Maybe some reboots here and there or my random port guessing. Eventually, it started working with port `8899` on both, HoloLens 1 and HoloLens 2.