title: Problems to receive messages from server to HoloLens
body: <p>I am developing an app which needs to connect to server in order to receive messages. I am using TCP/IP protocol because I need to establish a previous connection. Because of the problem with the System.Net.Sockets library I am using Windows.Networking.Socket library.</p>
  
  <p>The code to use the aforementioned protocol is as follows:</p>
  
  <pre><code>using System;
  using System.IO;
  using HoloToolkit.Unity;
  using UnityEngine;
  #if !UNITY_EDITOR
  using System.IO;
  using System.Threading.Tasks;
  using Windows.Networking;
  using Windows.Networking.Sockets;
  #endif
  
  /// &lt;summary&gt;
  /// TcpNetworkClientManager class provides methods to
  /// send and receive messages asynchronously
  /// &lt;/summary&gt;
  public class TcpNetworkClientManager : Singleton&lt;TcpNetworkClientManager&gt;
  {
      #region PRIVATE_MEMBERS_VARIABLES
      [SerializeField]
      [Tooltip("The Ipv4 address of the machine to connect")]
      private string ip = "127.0.0.1";
  
      [SerializeField]
      [Tooltip("The connection port on the machine to use")]
      private int port = 9999;
  
      /// &lt;summary&gt;
      /// The message we receive
      /// &lt;/summary&gt;
      private string message;
  
      /// &lt;summary&gt;
      /// Tracks if a client is connected
      /// &lt;/summary&gt;
      private bool clientConnected;
  
      /// &lt;summary&gt;
      /// Flag to receive message
      /// &lt;/summary&gt;
      private bool receiveFlag;
  
  #if !UNITY_EDITOR
      /// &lt;summary&gt;
      /// Supports network communication
      /// &lt;/summary&gt;
      StreamSocket socket;
  
      /// &lt;summary&gt;
      /// Stream of data to write
      /// &lt;/summary&gt;
      private StreamWriter writer = null;
  
      /// &lt;summary&gt;
      /// Stream of data to read
      /// &lt;/summary&gt;
      private StreamReader reader = null;
  #endif
      #endregion // PRIVATE_MEMBERS_VARIABLES
  
      #region PUBLIC_MEMBERS_VARIABLES
      public string Ip
      {
          get
          {
              return ip;
          }
          set
          {
              ip = value;
          }
      }
  
      public int Port
      {
          get
          {
              return port;
          }
          set
          {
              port = value;
          }
      }
  
      public string Message
      {
          get
          {
              return message;
          }
          set
          {
              message = value;
          }
      }
  
      public bool ClientConnected
      {
          get
          {
              return clientConnected;
          }
      }
      #endregion // PUBLIC_METHODS
  
      #region MONOBEHAVIOUR_METHODS
      protected override void Awake()
      {
          base.Awake();
          Message = string.Empty;
          clientConnected = false;
      }
      #endregion // MONOBEHAVIOUR_METHODS
  
      #region PUBLIC_METHODS
      /// &lt;summary&gt;
      /// Connects to a tcp server using TCP/IP protocol
      /// &lt;/summary&gt;
      public void Connect()
      {
          receiveFlag = true;
  #if !UNITY_EDITOR
          Task.Run(async () =&gt; {
              try
              {
                  socket = new StreamSocket();
                  await socket.ConnectAsync(new HostName(Ip), Port.ToString());
                  clientConnected = true;
                  writer = new StreamWriter(socket.OutputStream.AsStreamForWrite());
                  reader = new StreamReader(socket.InputStream.AsStreamForRead());
                  await Receive();
              }
              catch(Exception)
              {
                  Debug.Log("Unable to connecto to server");
              }
          });
  #endif
          Debug.LogWarningFormat("Tcp connect not supported in editor");
      }
  
      /// &lt;summary&gt;
      /// Sends asynchronously a message using TCP/IP protocol
      /// &lt;/summary&gt;
      /// &lt;param name="data"&gt;&lt;/param&gt;
      public void Send(string messageToSend)
      {
  #if !UNITY_EDITOR
          if (writer != null) Task.Run(async () =&gt;
          {
              try
              {
                  await writer.WriteAsync(messageToSend);
                  await writer.FlushAsync();
                  Debug.Log("Sent: " + messageToSend);
              }
              catch(Exception)
              {
                  Debug.Log("Unable to send message to server");
              }
          });
  #endif
          Debug.LogWarningFormat("Tcp send not supported in editor");
      }
  
      /// &lt;summary&gt;
      /// Frees resources both reader and writer
      /// &lt;/summary&gt;
      public void Close()
      {
  #if !UNITY_EDITOR
          receiveFlag = false;
  
          if (writer != null) writer.Dispose();
          writer = null;
  
          if (reader != null) reader.Dispose();
          reader = null;
  #endif
          Debug.LogWarningFormat("Tcp close not supported in editor");
      }
      #endregion // PUBLIC_METHODS
  
      #region PRIVATE_METHODS
      /// &lt;summary&gt;
      /// Receives asynchronously a message using TCP/IP protocol
      /// &lt;/summary&gt;
  #if !UNITY_EDITOR
      private async Task Receive()
      {
          if (reader != null)
          {
              while (receiveFlag)
              {
                  try
                  {
                      if (clientConnected)
                      {
                          string messageToReceive = await reader.ReadToEndAsync();
                          if (messageToReceive != null)
                          {
                              Message = messageToReceive;
                              Debug.Log("Received: " + message);
                          }
                      }
                  }
                  catch (Exception)
                  {
                      Debug.Log("Unable to receive from server");
                  }
              }
          }
      }
  #endif
      #endregion // PRIVATE_METHODS
  }
  </code></pre>
  
  <p>Although I can connect to the server, I do not receive anything. I know that the Receive() function reaches to</p>
  
  <pre><code>string messageToReceive = await reader.ReadToEndAsync();
  </code></pre>
  
  <p>but the next statements are not called, that is, the thread is waiting for data, but the data never is received.</p>
  
  <p>Does anyone know what trouble I am having?</p>
  
  <p>Thanks in advance,
  Cristian.</p>