title: Socket communication between server app and Matlab client using Java
body: <p>I have a written C++ server app that I would like to be able to control from Matlab. I have used a mex function for socket communication so far, but I would like to ditch the mex function and use inline Java directly in the m files. This will be a more streamlined solution.</p>
  
  <p>My C++ based standalone app expects a message with the following data in the following order . . .</p>
  
  <p>This part of the protocol is fixed and cannot be changed:</p>
  
  <ul>
  <li><p>uint32 magic_number - this is a magic number (445566) that must be at
  the start of the message or the rest of the message will be ignored. </p></li>
  <li><p>uint32 num_bytes -
  this is the number of bytes used for the rest of the message block
  (excluding this initial 8 bytes)</p></li>
  </ul>
  
  <p>This part of the protocol was designed by me and can be changed:</p>
  
  <ul>
  <li><p>Next comes a header made of 4 uint8 values (like an ipv4 address)
  signalling to the app what the following data represents (if any data follows)</p></li>
  <li><p>After this, the remaining bytes can represent many different things.
  Most commonly this would be a string (key value) followed by a long array of floating point values (audio data). However, there may just be a string, or they may just be an array of floating point values. The 4 uint8 values let the server know what to expect here.</p></li>
  </ul>
  
  <p>As you can see, I am currently squeezing everything into an array of uint8 (a colossal kludge). This is because the java "write" function expects a byte array and a Matlab uint8 array is a compatible data type as I found when using the following table on the Mathworks site <a href="http://www.mathworks.co.uk/help/techdoc/matlab_external/f6425.html" rel="nofollow">Passing Data to a Java Method</a> </p>
  
  <p>I'm not a Java programmer, but I have managed to get a very simple bit of communication code up and running this afternoon. Can anyone help me make this better?</p>
  
  <pre><code>import java.net.Socket
  import java.io.*
  
  mySocket = Socket('localhost', 12345);
  output_stream   = mySocket.getOutputStream;
  d_output_stream = DataOutputStream(output_stream);
  
  
  data = zeros(12,1,'uint8');
  
  %Magic key: use this combination of uint8s to make
  % a uint32 value of = 445566 -&gt; massive code-smell
  data(1) = 126;
  data(2) = 204;
  data(3) = 6;
  
  %Size of message block:
  %total number of bytes in following message including header
  %This is another uint32 i.e. (data(5:8))
  
  data(5) = 4;
  
  %header B: a group of 4 uint8s
  data(9) = 1;
  data(10) = 2;
  data(11) = 3;
  data(12) = 4;
  
  %Main block of floats
  %????
  
  
  d_output_stream.write(data,0,numel(data));
  
  
  pause(0.2);
  mySocket.close;
  </code></pre>
  
  <p>I have experimented with sending a java object composed of the different parts of the data that I would like to send, but I am not sure how they end up ordered in memory. In C/C++ it is very easy to append different data types in a contiguous block of memory and then send it. Is there a simple way for me to do this here in Java? I would eventually like to make the communications 2-way also, but this can wait for now. Thanks for reading.</p>