Code covered by the BSD License  

Highlights from
TCP/IP Socket Communications in MATLAB using Java Classes

4.83333

4.8 | 6 ratings Rate this file 63 Downloads (last 30 days) File Size: 3.35 KB File ID: #25249

TCP/IP Socket Communications in MATLAB using Java Classes

by

 

A simple example TCP/IP server and client using a helper Java class

| Watch this File

File Information
Description

Provides an example Server / Client written in MATLAB that utilises the ability to call Java to perform message communication using TCP/IP.

Similar to my previous example (http://www.mathworks.com/matlabcentral/fileexchange/21131) but allows for much more efficient data transmission by using a helper Java class to minimise function call overhead.

To use the example:

- Start 2 instances of MATLAB
- in the first execute the following:
  message = char(mod(1:1000, 255)+1);
  server(message, 3000, 10)
- in the second execute the following:
  javaaddpath('<path to where the .class file is located>');
  data = client('localhost', 3000)

For more details see:
http://iheartmatlab.blogspot.com/2009/09/tcpip-socket-communications-in-matlab.html

Acknowledgements

Tcp/Ip Socket Communications In Matlab inspired this file.

This file inspired Tcp/Ip Communications In Matlab.

MATLAB release MATLAB 7.8 (R2009a)
Other requirements Although I have developed it in MATLAB 2009a, it should work on versions as far back as R14.
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (11)
11 Dec 2014 Rodney Thomson

Hi Jacobo, I have used these functions to read 16-channel 96kHz sampled 16-bit audio before without issue.

If you are attempting to read the data 1000 times per second then you may have a problem. Read more data less ofter to avoid function call overhead. For example, I read the acoustic data in 0.25 second chunks (24000 samples at a time).

Use the MATLAB profiler tool to see where your code is spending its time. Also, is it using high CPU load or high network load at this time? The issue could be in a number of places.

10 Dec 2014 Jacobo

I used these functions and work well. They are very easy to adapt. But I'm having a problem receiving data fast enough (~100 Double @ 1000Hz) ther is anyway to solve this?

07 Nov 2014 Sergio  
02 Oct 2013 Rodney Thomson

Bi-directional coms can be done by a single socket, although you still need a 'server' and a 'client'. Someone needs to wait for a connection, and the other needs to initiate the connection.

When you get the Java Socket() object, you can get both an InputDataStream and an OutputDataStream (or even a DataStream) and then read/write with the relevant stream.

30 Sep 2013 Tiago

In order to have bi-directional communication I have to create 2 Sockets? or is there any other option (sockets are bi-directional by default)?

27 Sep 2013 Tiago

My bad!
I read 'addpath' instead of 'javaaddpath'.
Thanks for the answer

27 Sep 2013 Rodney Thomson

Tiago:
Don't forget to add the directory containing DataReader.class to the java path:
>> javaaddpath('<path to where the .class file is located>');

(as mentioned above and on the blog page).

26 Sep 2013 Tiago

The server manage to get a connection with the client but I'm having an error in:
data_reader = DataReader(d_input_stream);

Undefined function 'DataReader' for input arguments of type 'java.io.DataInputStream'.

Any ideas?

09 Aug 2012 Sebastian Holmqvist

Excellent! Makes a world of difference when handling string > 1 MB. I use Kevin Bartlett's "TCP/IP Communications in Matlab" (http://www.mathworks.se/matlabcentral/fileexchange/24524-tcpip-communications-in-matlab) which can optionally utilize your java classes and it's lizard fast!

19 Jan 2012 Ueli Rutishauser  
16 Nov 2009 Kevin Bartlett  

Contact us