File Exchange

image thumbnail

TCP/IP Socket Communications in MATLAB using Java Classes

version 1.0 (3.35 KB) by

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

4.625
8 Ratings

12 Downloads

Updated

View License

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

Comments and Ratings (13)

Tu Nguyen Anh

khaja

khaja (view profile)

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.

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?

Sergio

Sergio (view profile)

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.

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

Tiago

Tiago (view profile)

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

Rodney Thomson

Rodney Thomson (view profile)

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

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 'java.io.DataInputStream'.

Any ideas?

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!

Kevin Bartlett

MATLAB Release
MATLAB 7.8 (R2009a)
Tags Add Tags

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

» Watch video