Search Unity

HoloLens TCP Socket No Connection

Discussion in 'VR' started by unity_F5j4joJ2hAy9vQ, Jan 10, 2019.

  1. unity_F5j4joJ2hAy9vQ

    unity_F5j4joJ2hAy9vQ

    Joined:
    Dec 6, 2018
    Posts:
    1
    i have been trying for some days now to create a TCP connection from a HoloLens to my local machine. I know that there are many solutions out there, but i have no clue why i cannot connect to the device. Maybe anyone could help me or advise me what i am doing wrong.

    Here is my Code so far:
    Code (CSharp):
    1. using UnityEngine;
    2. #if WINDOWS_UWP
    3. using System;
    4. using System.Collections;
    5. using System.Collections.Generic;
    6. using System.IO;
    7. using System.Threading.Tasks;
    8. using Windows.Networking;
    9. using Windows.Networking.Sockets;
    10. using Windows.Storage.Streams;
    11. using Windows.Foundation;
    12. #endif
    13.  
    14. public class DataListener : MonoBehaviour
    15. {
    16. #if WINDOWS_UWP
    17.  
    18.   private StreamSocket _socket;
    19. #endif
    20.  
    21.   private void Start()
    22.   {
    23.     SetupTcpConnection();
    24.   }
    25.  
    26.   //HoloLens Code
    27.   private void SetupTcpConnection()
    28.   {
    29. #if WINDOWS_UWP
    30.  
    31.     if (_socket != null)
    32.       return;
    33.  
    34.     _socket = new StreamSocket();
    35.  
    36.     var hostName = new HostName("192.168.8.102");
    37.     var socketConnectionAwaiter = _socket.ConnectAsync(hostName, "11000");
    38.     var handler = new                     AsyncActionCompletedHandler(SocketConnectionCompleted);
    39. socketConnectionAwaiter.Completed = handler;
    40. #endif
    41.  
    42.   }
    43. #if WINDOWS_UWP
    44.  
    45.   void SocketConnectionCompleted(IAsyncAction action, AsyncStatus status)
    46.   {
    47.     if (status != AsyncStatus.Completed)
    48.     {
    49.       _socket.Dispose();
    50.       _socket = null;
    51.       return;
    52.     }
    53.  
    54.     // Read the data
    55.     using (var reader = new     StreamReader(_socket.InputStream.AsStreamForRead()))
    56.     {
    57.       var data = reader.ReadToEnd();
    58.  
    59.       //then read data
    60.  
    61.     }
    62.   }
    63. #endif
    64.  
    65. }

    C# Application Code:

    Code (CSharp):
    1. public static void StartClient3()
    2. {
    3.     var listener = new
    4.     TcpListener(IPAddress.Any, 11000);
    5.     var connectionHandler = new ConnectionHandler();
    6.  
    7.     var workerThread =
    8.         new Thread(connectionHandler.HandleConnections);
    9.     workerThread.Start(listener);
    10.  
    11.     while (!Console.KeyAvailable) { }
    12.  
    13.     Console.WriteLine("Shutting down");
    14.     listener.Stop();
    15.     workerThread.Abort();
    16. }
    17.  
    18. public class ConnectionHandler
    19. {
    20.     TcpListener _listener;
    21.  
    22.     public void HandleConnections(object dataToPass)
    23.     {
    24.  
    25.         _listener = dataToPass as TcpListener;
    26.         if (_listener == null)
    27.             return;
    28.  
    29.         _listener.Start();
    30.  
    31.         Console.WriteLine("We are waiting for a connection");
    32.         while (true)
    33.         {
    34.             var client = _listener.AcceptTcpClient();
    35.  
    36.             Console.WriteLine("We are connected");
    37.  
    38.             var dataToSend = "SampleString";
    39.             using (var writer = new StreamWriter(client.GetStream()))
    40.             {
    41.                 writer.Write(dataToSend);
    42.                 writer.Flush();
    43.             }
    44.             client.Close();
    45.         }
    46.     }
    47. }
    I get the IP (192.168.8.102) from my local machine from ipconfig, so this should be the right one. I cannot simply connect to HoloLens and I have no idea why.

    I also checked the settings in Unity like InternetClientServer, PrivateNetworkClientServer, InternetClient.

    Is there anything what I forget. I would really appreciate your Help!

    Cheers, Pat
     
  2. yuejiework

    yuejiework

    Joined:
    Mar 6, 2019
    Posts:
    2
    I have same problem.
     
  3. karinarigby

    karinarigby

    Joined:
    Jun 14, 2018
    Posts:
    1
    Did you ever figure this out?
     
  4. JeffenHuang

    JeffenHuang

    Joined:
    Sep 9, 2020
    Posts:
    2
    Same issue here
     
  5. Shaunyowns

    Shaunyowns

    Joined:
    Nov 4, 2019
    Posts:
    328
    Hey there!

    There seems to be a related forum post here, where a user had found a solution. Can you see if this resolves your issues?