Code covered by the BSD License  

Highlights from
A simple UDP communications application

4.8 | 12 ratings Rate this file 102 Downloads (last 30 days) File Size: 2.89 KB File ID: #24525 Version: 1.2

A simple UDP communications application


Kevin Bartlett (view profile)


22 Jun 2009 (Updated )

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

| Watch this File

File Information

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


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 (37)
28 Jul 2015 Kevin Bartlett

Kevin Bartlett (view profile)

@Gus--Good news! I'm gradually learning that it is almost ALWAYS a firewall that is to blame in these situations. Turning off the firewall briefly is a really good test to see if it is the source of a problem.

Comment only
28 Jul 2015 Gus Lott

Solved. A recent update to McAfee Internet Security resulted in blocking high UDP ports. It did not block TCP ports. JUDP now receives the proper broadcast message. Thanks for your firewall suggestion.

Comment only
27 Jul 2015 Kevin Bartlett

Kevin Bartlett (view profile)

@Gus--Yes, but the thing doing the sending is sending to, isn't it? Can you change this?

Comment only
25 Jul 2015 Gus Lott

Not trying to send - just receive the broadcast on port 4992.
mssg = judp('receive',4992,266,3000);
It always times out.

Comment only
23 Jul 2015 Kevin Bartlett

Kevin Bartlett (view profile)

@Gus--From your Wireshark screenshot ( it looks like you are trying to send to the special broadcast address, ( Have you tried sending to the actual IP address of the destination computer instead of the broadcast address?

Comment only
23 Jul 2015 Gus Lott

I removed the DAX software that was listening on the same port. Once that was removed, I ran the test you suggested.
I started MATLAB session 1:
mssg = judp('receive',4991,10,5000); char(mssg')
Then execute in MATLAB session 2:
And session 1 shows:
ans =
So loopback is working.
However, WireShark still shows this computer receiving the port 4992 packet I described in the question, but still cannot see it using JUDP.
mssg = judp('receive',4992,266,3000); char(mssg')
Error using judp (line 166)
judp.m--Failed to receive UDP packet; connection timed out.

Comment only
22 Jul 2015 Kevin Bartlett

Kevin Bartlett (view profile)

@Gus--when you say that "localhost works without an issue", what does that mean? The phrase suggests to me that judp send/receive works as long as you use the loopback address, but that would seem to contradict your first post on July 21. Please clarify.

Comment only
21 Jul 2015 Gus Lott

Localhost works without an issue. I have not submitted a community question about why I am not seeing a UDP broadcast. See:
Would appreciate any help to discover what's wrong.

Comment only
21 Jul 2015 Kevin Bartlett

Kevin Bartlett (view profile)

@Gus--I think that BindException means that the port you are trying to use is already taken. Sounds like your "DAX" software is using the same port as judp, which is quite a coincidence. Have you tried using a different port numbers than 21566? It can be any integer between 1025 and 65535, so there are lots to choose from. I guess that still wouldn't explain why you're getting a timeout with DAX disabled, but it might be worth trying.

Comment only
21 Jul 2015 Gus Lott

Thanks Kevin. Your excellent routine has worked before. Am back to it, and running into issues.

Discovered the problem producing the error given. When the FlexRadio SmartSDR DAX.exe is running, I get the " When I turn it off, receive times out.

With the DAX software disabled, I followed your suggestion. Started this in one session:
mssg = judp('receive',21566,10,5000);
Then immediate did this in another session:

Got this result on the receive session:
Error using judp (line 166)
judp.m--Failed to receive UDP packet; connection timed out.

Will continue troubleshooting. All other software on computer talks TCP & UDP properly.

Comment only
21 Jul 2015 Kevin Bartlett

Kevin Bartlett (view profile)

@Gus--I don't have access to a 2015 version of Matlab, so I can't really troubleshoot your problem. I will say, though, that the same symptoms could arise from a networking issue or a change in firewall settings. A good test is to have two Matlab sessions running on the same computer. Use one session to send the UDP packet using the "loopback" IP address of Use the other Matlab session to receive the packet. Because the communications are internal to the same computer, this bypasses the network. I'm not sure if this would make a difference, but you could try this test with your computer's firewall off, as well.

Comment only
20 Jul 2015 Gus Lott

MATLAB Version: (R2015a)
Java Version: Java 1.7.0_60-b19

Has something changed in latest Matlab or Java versions? This used to work, listening on port 4992 for a broadcast.


>> [mssg,sourceHost] = judp('receive',4991,4096,5000)
Error using judp (line 166)
judp.m--Failed to receive UDP packet.
Java error message follows:
Java exception occurred: Address already in use: Cannot bind
at Method)
at Source)
at Source)
at Source)
at<init>(Unknown Source)
at<init>(Unknown Source)
at<init>(Unknown Source)

Comment only
19 Feb 2015 Peter

Peter (view profile)

27 May 2014 exosceleton

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

Comment only
26 May 2014 Kevin Bartlett

Kevin Bartlett (view profile)

@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 You should be able to use 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.

Comment only
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 takes minutes.

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


21 Mar 2014 Manoj

Manoj (view profile)

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.

Comment only
11 Mar 2014 Keane Gonzalez  
03 Feb 2014 Kevin Bartlett

Kevin Bartlett (view profile)

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

Comment only
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

Comment only
27 Oct 2013 Chacha

Chacha (view profile)

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

Kevin Bartlett (view profile)

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

Comment only
11 Jul 2013 Victoria Emberle

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

Comment only
05 Jun 2013 Kevin Bartlett

Kevin Bartlett (view profile)

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?

Comment only
05 Jun 2013 Gabriel

Gabriel (view profile)

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.

Comment only
31 May 2013 Yali

Yali (view profile)

thanks very much for sharing this copy.

26 Feb 2013 Kevin Bartlett

Kevin Bartlett (view profile)

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

Comment only
26 Feb 2013 jawad

jawad (view profile)

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

Comment only
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!

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

23 May 2011 Kevin Bartlett

Kevin Bartlett (view profile)

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

Comment only
22 May 2011 Jveer

Jveer (view profile)

21 Apr 2011 Rahul

Rahul (view profile)

Is it faster than the intrument control toolbox command UDP?

Comment only
24 Aug 2010 Tanyer Alan

Tanyer Alan (view profile)

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!

29 Mar 2010 1.1

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 1.2

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

Contact us