View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

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

4.6 | 8 ratings Rate this file 30 Downloads (last 30 days) File Size: 3.35 KB File ID: #25249 Version: 1.0

TCP/IP Socket Communications in MATLAB using Java Classes


Rodney Thomson (view profile)


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

| Watch this File

File Information

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 ( 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:


Tcp/Ip Socket Communications In Matlab inspired this file.

This file inspired Tcp/Ip Communications In Matlab and Tra Ci4 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 (13)
20 Sep 2016 Tu Nguyen Anh

17 Apr 2016 khaja

khaja (view profile)

11 Dec 2014 Rodney Thomson

Rodney Thomson (view profile)

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.

Comment only
10 Dec 2014 Jacobo

Jacobo (view profile)

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

Sergio (view profile)

02 Oct 2013 Rodney Thomson

Rodney Thomson (view profile)

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.

Comment only
30 Sep 2013 Tiago

Tiago (view profile)

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)?

Comment only
27 Sep 2013 Tiago

Tiago (view profile)

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

27 Sep 2013 Rodney Thomson

Rodney Thomson (view profile)

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).

Comment only
26 Sep 2013 Tiago

Tiago (view profile)

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 ''.

Any ideas?

Comment only
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" ( which can optionally utilize your java classes and it's lizard fast!

19 Jan 2012 Ueli Rutishauser

16 Nov 2009 Kevin Bartlett

Kevin Bartlett (view profile)

Contact us