title: how unity can read data from Matlab socket
body: <p>i want to do some simulation in unity. for my project i want to use Matlab as processing layer. so i want to use socket for communications. </p>
  
  <p><strong>here Matlab is server and this is the code:</strong> </p>
  
  <pre><code>clc
  clear all
  tcpipServer = tcpip('0.0.0.0',55000,'NetworkRole','Server');
  while(1)
      b=1
  data = membrane(1);
  b=2
  s = whos('data');
  b=3
  set(tcpipServer,'OutputBufferSize',s.bytes);
  b=4
  fopen(tcpipServer);
  b=5
  fwrite(tcpipServer,'hi');
  fclose(tcpipServer);
  d=6
  end
  </code></pre>
  
  <p>i use 1,2,.. for showing the process</p>
  
  <p><strong>i use this code in unity</strong></p>
  
  <pre><code>using UnityEngine;
  using System.Collections;
  using System.Net;
  using System.Net.Sockets;
  using System;
  using System.IO;
  
  
  public class Socket : MonoBehaviour {
  
      // Use this for initialization
  
      internal Boolean socketReady = false;
      TcpClient mySocket;
      NetworkStream theStream;
      StreamWriter theWriter;
      StreamReader theReader;
      String Host = "localhost";
      Int32 Port = 55000;
  
  
      void Start () {
          setupSocket ();
          Debug.Log ("socket is set up");
          Debug.Log (readSocket()+" is the message");
          closeSocket ();
          Debug.Log ("Socket is closed");
      }
  
      // Update is called once per frame
      void Update () {
  
      }
  
      public void setupSocket() {
          try {
              mySocket = new TcpClient(Host, Port);
              theStream = mySocket.GetStream();
              theWriter = new StreamWriter(theStream);
              theReader = new StreamReader(theStream);
              socketReady = true;
          }
          catch (Exception e) {
              Debug.Log("Socket error: " + e);
          }
      }
  
      public String readSocket() {
          if (!socketReady)
              return "";
          if (theStream.DataAvailable)
              return theReader.ReadLine();
          return "";
      }
  
  
      public void closeSocket() {
          if (!socketReady)
              return;
          theWriter.Close();
          theReader.Close();
          mySocket.Close();
          socketReady = false;
      }
  }
  </code></pre>
  
  <p>at first i start Matlab then i play unity. the result is, the connection is done properly but i can not read the data Matlab send </p>
  
  <p><strong>this is what i get in Matlab</strong></p>
  
  <pre><code>b =
  
       1
  
  
  b =
  
       2
  
  
  b =
  
       3
  
  
  b =
  
       4
  
  
  b =
  
       5
  
  
  d =
  
       6
  
  
  b =
  
       1
  
  
  b =
  
       2
  
  
  b =
  
       3
  
  
  b =
  
       4
  </code></pre>
  
  <p><strong>and finally this is what i get in unity</strong></p>
  
  <pre><code>socket is set up
  
  is the message
  
  Socket is closed
  </code></pre>
  
  <p><strong>the expected result is</strong></p>
  
  <pre><code>socket is set up
  
  hi is the message
  
  Socket is closed
  </code></pre>
  
  <p>i dont know how to read data and how to implement it.</p>
  
  <p>thank you</p>