Code covered by the BSD License  

Highlights from
TCP/IP Socket Communications in MATLAB

5.0

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

TCP/IP Socket Communications in MATLAB

by Rodney Thomson

 

19 Aug 2008 (Updated 19 Aug 2008)

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 submission has inspired the following:
A simple UDP communications application
MATLAB release MATLAB 7.0.4 (R14SP2)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (11)
21 Aug 2008 Felipe Posada

Very useful, thank you.

05 Sep 2008 Jorge Ramírez

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

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?

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
  

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

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!

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?

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

02 Feb 2012 Sharath

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

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.

Please login to add a comment or rating.
Tag Activity for this File
Tag Applied By Date/Time
example Rodney Thomson 22 Oct 2008 10:15:13
application Rodney Thomson 22 Oct 2008 10:15:13
socket Rodney Thomson 22 Oct 2008 10:15:13
tcpip Rodney Thomson 22 Oct 2008 10:15:13
test Rodney Thomson 22 Oct 2008 10:15:13
applications Rodney Thomson 22 Oct 2008 10:15:13
measurement Rodney Thomson 22 Oct 2008 10:15:13
how does client send datacommand to server Eric 24 Mar 2010 00:01:25
how does client send datacommand to server Rodney Thomson 19 Jun 2010 10:33:01
application Zaiming 20 May 2011 11:42:07

Contact us at files@mathworks.com