Code covered by the BSD License  

Highlights from
A simple UDP communications application

4.81818

4.8 | 11 ratings Rate this file 89 Downloads (last 30 days) File Size: 2.89 KB File ID: #24525

A simple UDP communications application

by

 

22 Jun 2009 (Updated )

Sends/receives UDP packets using Matlab's Java interface.

| Watch this File

File Information
Description

UDP packets are used to send data from one computer to another over a network or from one application to another within a single computer.

The judp.m program uses Matlab's ability to call Java code to enable it to send and/or receive UDP packets. One Matlab session can communicate with another Matlab session (on the same machine or over the network) or it can communicate with a completely different program (again, on the same machine or over the network).

Acknowledgements

Tcp/Ip Socket Communications In Matlab inspired this file.

MATLAB release MATLAB 7.8 (R2009a)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (24)
27 May 2014 exosceleton

@Kevin Bartlett
That seems to be the smart way indeed. Thanks

26 May 2014 Kevin Bartlett

@exosceleton--My guess would be that Matlab's java machine is not doing garbage collecting properly (I've seen similar problems with Matlab's implementation of database cursor objects). So when you go through 256 IP addresses, you're creating 256 Java Socket objects, and they're clogging up Matlab's allocated Java space and slowing down your computer. As a workaround, why not send a single packet out to your network's broadcast address (see http://en.wikipedia.org/wiki/Broadcast_address)? You should be able to use 255.255.255.255 in your call to jupd.m. All the machines on your network will receive the UDP packet, but only the machine you're looking for will know how to respond to the specific UDP packet you send. Once this "handshake" is complete, the two computers will know each other's IP addresses, and you will no longer need to use the broadcast address.

26 May 2014 exosceleton

If i enter a static ip of the destination i can send&recieve at a quick rate (multiple cycles per second).

However when i change the ip address in a loop in an attempt to find the the correct device ip in a DHCP environment the cycle time quickly increases to multiple seconds per send&recieve command.

looking for the device from ip xxx.xxx.xxx.1:255 takes minutes.

is there a reason for this change in processing time between a fixed ip or multiple ip destinations?

Thanks

21 Mar 2014 Manoj

When I send UDP packets using this script, the destination UDP port number stays fixed to the one passed in the argument but source UDP port number seems to be incrementing by 1 every time I call judp. Is there a way to have a fix source UDP port number as well?
Thanks in advance.

11 Mar 2014 Keane Gonzalez  
03 Feb 2014 Kevin Bartlett

@Adam: actionStr is an input argument that can be either the string 'send' or the string 'receive'. Have you tried following the examples that are displayed when you type 'help judp'? They should work pretty much as-is, except that when you use 'send', you will have to supply the actual IP address of your target, and in both the 'send' and 'receive' cases, you will need to supply an integer port number between 1025 and 65535. Note that the specified port must be open in the receiving machine's firewall.

03 Feb 2014 Adam Broadhurst

Hi, with regards to the script how would I go about defining the actionStr input argument? Having some trouble getting the script up and running.

Thanks in advance

27 Oct 2013 Chacha

AMAZING! This code was exactly what I needed for quickly sending small packets between two computers, both sending and receiving. Using test code in a small loop I was able to gt loops that took on average around 10 ms. I couldn't believe it, since the computers I was testing this with were relatively slow.

(p.s. i want to give give code 5 stars, but the website's java isn't working so it won't let me submit my rating.)

11 Jul 2013 Kevin Bartlett

@Victoria--actionStr is an input argument that can be either the string 'send' or 'receive'.

11 Jul 2013 Victoria Emberle

What is actionStr because it is undefined in the version of matlab 2006a

05 Jun 2013 Kevin Bartlett

Sorry, Gabriel, I don't know how a multicast packet differs from a regular UDP packet. Can't you just receive them with judp() like a regular UDP packet?

05 Jun 2013 Gabriel

Hi, thanks for this code. Can you submit a version that supports multicast? I am a novice user at this stuff but really need to read in a UDP multicast packet.

31 May 2013 Yali

thanks very much for sharing this copy.

26 Feb 2013 Kevin Bartlett

@jawad--If you type "help judp" at the Matlab command line, you should see three simple examples that ought to work as a starting point. The third example is simplest because it uses 'localhost' as the destination for the udp packet; this means you can run both the 'receive' and 'send' judp commands from the same computer (in separate Matlab sessions). Just make sure the specified port, 21566, is open in your firewall.

26 Feb 2013 jawad

Can any one guide me how i will be able to run this code step by step.

30 Oct 2012 Allister Kay

Hey. A great programme. I have one remark though. If one has many iterations of the function, let's say in a while-loop receiving realtime udp-packages the socket is opened and closed in every call. This is not criticism, I think an experienced MATLAB programmer will have no problem redesigning the code. BTW, if anyone has a solution for my problem I would be very grateful as my MATLAB skills are limited.

08 Aug 2012 che adil

hello Kiven,
I saw your "judp.m" after having been proposed by a friend to solve a problem of image acquisition of a camera-ip using Matlab.
I wonder if your function "judp.m" able to make this acquisition?
if yes, then how can we do this?
I thank you in advance!

remark!
- I work in matlab 2010a
- The camera used is "Teltonika - ip-camera-H264, model: camera3G MVC200

23 May 2011 Kevin Bartlett

@Rahul--judp.m is really not that sophisticated a program, so I suspect it is slower than the equivalent command from the Instrument Control Toolbox. The trouble with the Mathworks' UDP/TCP communications programs is that they ARE sophisticated--they may be fast and highly flexible, but they are also very complicated to use (they are also considerably more expensive than judp.m).

22 May 2011 Jveer  
21 Apr 2011 Rahul

Is it faster than the intrument control toolbox command UDP?

24 Aug 2010 Tanyer Alan  
29 Apr 2010 Peter Manley-Cooke

Very good. It all works and it provided me with a starting point for what I wanted to do.

26 Feb 2010 Adam Becker

To quickly fix the Ctrl-C issue just add "socket.setReuseAddress(1)"

28 Jul 2009 Jeff Barton

The utility performs exactly as advertised, and is simple to use. (And very timely! It met my needs perfectly.)

The only issue I have noticed is that it (often) does not handle Ctrl-C breaks very gracefully... If I Ctrl-C during a call to judp() to receive a UDP message on a specified port, I can no longer receive on that same port. (I get a "cannot bind" error during subsequent calls to judp() on that same port.) Other than that, great submission!

Updates
29 Mar 2010

Added socket.setReuseAddress(1) in order to make reuse of port numbers more reliable after pressing ctrl-C (thanks to Adam Becker for the suggestion). Also tidied up some MLint messages.

29 Mar 2010

Forgot to attach new version of judp.m on last update.

Contact us