title: Matlab sockets wait for response
body: <p>I'm trying to run the following client and server socket example code in matlab:
  <a href="http://www.mathworks.com/help/instrument/using-tcpip-server-sockets.html" rel="nofollow">http://www.mathworks.com/help/instrument/using-tcpip-server-sockets.html</a></p>
  
  <p>This is my code.</p>
  
  <p>Server:</p>
  
  <pre><code>t=tcpip('0.0.0.0', 9994, 'NetworkRole', 'server');
  fopen(t);
  data=fread(t, t.BytesAvailable, 'double');
  plot(data);
  </code></pre>
  
  <p>Client:</p>
  
  <pre><code>data=sin(1:64);
  t=tcpip('localhost', 9994, 'NetworkRole', 'client');
  fopen(t);
  fwrite(t, data, 'double');
  </code></pre>
  
  <p>This is what happens: I run the server code-> The program waits for the connection from the client-> I run the client code ->In the server console I get:</p>
  
  <pre><code>Error using icinterface/fread (line 163)
  SIZE must be greater than 0.
  
  Error in socketTentativaMatlab (line 3)
  data=fread(t, t.BytesAvailable, 'double');
  </code></pre>
  
  <p>What am I doing wrong? It looks like the server doesn't wait for the client to send anything to try to read the data, so there's no data to read (It waits for the client connection thought).  </p>
  
  <hr>
  
  <p><strong>Edit1:</strong>
  Ok, I'm sending chars now, so we know for sure that t.BytesAvaiable = number of elements.</p>
  
  <p>I have been able to successfully receive synchronously in the following way (this is server code, client code is the same but I send chars now and pause 1 second after establishing the connection with the server):</p>
  
  <pre><code>t=tcpip('0.0.0.0', 30000, 'NetworkRole', 'server');
  fopen(t);
  data=strcat(fread(t, 1, 'uint8')');
  if get(t,'BytesAvailable') &gt; 1
      data=strcat(data,fread(t, t.BytesAvailable, 'uint8')');
  end
  data
  </code></pre>
  
  <p>This is because I suspected that bytesAvaiable is the number of bytes left to read after attempting to read at least once... this doesn't seem very logical, but it apparently is what happens. Since I have to read at least once to know how many bytes the message has...I choose to read 1 byte only the first time. I then read what's left, if there is something left...</p>
  
  <p>I can make this work between matlab processes, but I can't do it between C++ and matlab. The C++ client successfully connects to the matlab server, and can send the data without problems or errors. However, on the matlab server side, I can't read it.</p>
  
  <p>Something seems very wrong with all this matlab tcpip implementation!</p>
  
  <hr>
  
  <p><strong>Edit2:</strong>
  If I properly close all the sockets in both client and server (basically don't let the program exit with open sockets), the above code seams to work consistently. I went to console and typed "netstat" to see all the connections ...It turns out since I was leaving open sockets, some connections were in the FIN_WAIT_2 state, which apparently rendered the ports of those connections unusable. Eventually the connection times out definitely, but it takes a minute or more, so, it's really best practice to just make sure the sockets are always properly closed.</p>
  
  <p>I don't understand thought what is the logic behind t.BytesAvaiable... it doesn't seam to make much sense the way it is. If I loop and wait for it to become greater then 0, it eventually happens, but this is not the way things are supposed to be with synchronous sockets. My code lets one do things synchronously, even though I don't understand why t.BytesAvaiable isn't properly set the first time.</p>
  
  <p>Final server code:</p>
  
  <pre><code>t=tcpip('0.0.0.0', 30000, 'NetworkRole', 'server');
  fopen(t);
  
  data=strcat(fread(t, 1, 'uint8'));
  if get(t,'BytesAvailable') &gt; 1
      data=strcat(data,fread(t, t.BytesAvailable, 'uint8')');
  end
  
  fclose(t);
  </code></pre>
  
  <p>Final client code:</p>
  
  <p>Your typical socket client, implemented in any language, but you will have to make sure that between successive calls of send() method/function (or between calling connect() and send()), at least 100ms (lower number seam to be risky) are ellapsed. </p>