5.0

5.0 | 3 ratings Rate this file 123 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)

Code covered by the BSD License  

A simple example TCP/IP server and client

Download Now | 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)
Zip File Content  
Other Files server.m,
client.m
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (5)
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

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
measurement Rodney Thomson 22 Oct 2008 10:15:13
applications Rodney Thomson 22 Oct 2008 10:15:13
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com