Code covered by the BSD License  

Highlights from
TCP/IP Socket Communications in MATLAB

5.0

5.0 | 4 ratings Rate this file 102 Downloads (last 30 days) File Size: 2.26 KB File ID: #21131

TCP/IP Socket Communications in MATLAB

by

 

19 Aug 2008 (Updated )

A simple example TCP/IP server and client

| Watch this File

File Information
Description

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

Importantly it does not require any pre-compiled DLLs or force you to compile some MEX c-files. Nor does it require any additional toolboxes Its all contained within the .m files.

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:
  data = client('localhost', 3000)

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

Acknowledgements

This file inspired Tcp/Ip Socket Communications In Matlab Using Java Classes, Tcp/Ip Communications In Matlab, and A Simple Udp Communications Application.

MATLAB release MATLAB 7.0.4 (R14SP2)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (17)
16 Jan 2013 Rodney Thomson

The next entry on the blog:
http://iheartmatlab.blogspot.com.au/2009/09/tcpip-socket-communications-in-matlab.html

Details using compiled Java class for pulling larger chunks of data from the socket at a time, returning them as MATLAB compatible arrays. It effectively wrappers the DataInputStream::read() method but returns an array, instead of taking an array by reference (which MATLAB cannot do)

15 Jan 2013 kinor

Hi Rodney,

thanks a lot for your fast reply.
The range of 1 -2s is what i achieved. Keeping the connections lets it drop to 0.1 s.
You say "Also the client.m code reads bytes 1 at a time which is very slow. See some of my other code examples on how to do this differently."
the link above just gives this one, where can I find them?

thanks a lot in advance
Kinor

15 Jan 2013 Rodney Thomson

Kinor:
With your specific example, I'd expect latency on average of between 1 and 2 seconds.

That is because the server is setup and a client polls to connect for each 'ping' and 'pong'. That takes time.

What you should do is use the code inside client.m and server.m AS AN EXAMPLE and tailor it to suit your application.

IE it looks like you have bi-directional comms. You could setup one machine as server and another as client, then use the Java DataOutputStream/DataInputStream objects on both the client and server to both read and write data without re-establishing a connection each time.

Also the client.m code reads bytes 1 at a time which is very slow. See some of my other code examples on how to do this differently.

14 Jan 2013 kinor

Hi Rodney,

which latency do you achieve with your nice functions?

i.e.
first started,
session1:
data = client('localhost', 3000, -1)
message = '3001 message';
server(message, 3001, -1)

then,
session2:
tic
message = '3001 message';
server(message, 3000, -1)
data = client('localhost', 3001, -1)
toc

thanks for your fine functions kinor

04 Jan 2013 Rodney Thomson

jawad - Yes you can. Simply run 2 instances of MATLAB and run 'client' in one, and 'server' in the other.

03 Jan 2013 jawad

Dear rodney thomson.
can we run the client.m and server.m on the single machine simultaneously.

03 Feb 2012 Rodney Thomson

Sharath,

To receive data continuously, you will need to call read() many times after you have successfully connected to the server.

For high update scenarios, you may want to use the Java class approach (http://iheartmatlab.blogspot.com.au/2009/09/tcpip-socket-communications-in-matlab.html) to read the data in chunks as I doubt you would be able to keep up with 1000Hz data otherwise.

02 Feb 2012 shaz

if i am getting data continuosly say for every 1milli sec from server to client how do we handle this

19 Jul 2011 Bebbe

Hello Eric .. I read your problem and I need to communicate with clients and servers in bidirectional mode ... how did you solve your problem? I am not a java expert so I am a bit in trouble ...
sorry for my bad English ..
thanks a lot ..

04 Jun 2010 Jens

Hey Eric, my client have to send data to the server. Could you give me some help and tell me the way you solved the problem?

01 Apr 2010 Eric

I have solved my problem. Just write here to share with everyone. I also read a lot java documentations about socket. Finally i think this program is most simple and suitable for our use. We can change it to a bidirectional program according to our requests. This is example is very important. Thanks!

24 Mar 2010 Eric

Thanks s lot for Rodney effort.
But in my current project, I need to use socket to communicate with a phycial layer model. So I need to write and read from the model via socket. The example only gives one way direction communication, i.e. client read data from the server. So i hope to know: how does the client send data to server by socket ?
I copy some commands from the server:

output_stream = output_socket.getOutputStream;
d_output_stream = DataOutputStream(output_stream);

% output the data over the DataOutputStream
% Convert to stream of bytes
fprintf(1, 'Writing %d bytes\n', length(message))
d_output_stream.writeBytes(char(message));
d_output_stream.flush;

And apply these in sending data to server, but i found this doesn't work, i.e. the client can't send data to server. Could you please give me some help? Thanks very much.

14 Apr 2009 Ricardo Correia

Hello! I have one question.. Can i send wav files through this? Or this is only to send messages?
Thanks for your help

24 Mar 2009 Rodney Thomson

Hi Maurizio,

Unfortunately the read(byte []) method will not work directly from within MATLAB. My thoughts as to why:

When you execute:
data = zeros(1, 100, 'char');
data_input_stream.read(data, 0, 100);
It supplies a COPY of the data array which would be populated. Unfortunately you don't have access to this. This requires pass-by-reference semantics which I don't think MATLAB currently support.

To get around this issue personally I've created helper Java classes to read blocks of data and RETURN the Byte array. This requires creating Java classes and compiling them.

Email me if you want some examples.

Rod

19 Mar 2009 Maurizio

It's very useful to build a simple network with MatLab, but in the example you read abyte in a loop. I've tried to read the buffer with the method read (byte [ ] b, int off, int len) of the class DataInputStream, but I don't find anything into the buffer b; the result of this method (the of bytes read) is correct! How can I pass the input buffer to the method?

05 Sep 2008 Jorge Ramírez

Thanks, it is very easy to use and very useful.

21 Aug 2008 Felipe Posada

Very useful, thank you.

Contact us