Skip to content

Instantly share code, notes, and snippets.

@dalegaspi
Created June 1, 2015 12:53
Show Gist options
  • Save dalegaspi/1ee0cfdeef8a919b6e14 to your computer and use it in GitHub Desktop.
Save dalegaspi/1ee0cfdeef8a919b6e14 to your computer and use it in GitHub Desktop.
UDP Multicast on Java

Introduction

There is very little information on the internet on how to send broadcast UDP messages in Java, which is kind of surprising since UDP broadcast is the simplest way to do broadcast messages without having to write a lot of code and or put in a lot of moving pieces, so long as one is aware of the drawbacks. I found myself having to search for this information and even Oracle's own documentation on this is just mediocre at best. Fortunately, someone took the time to write down. This is just a mirror of that information on GitHub to make it easier to find for someone searching for the same information.

The Sender

  1. Import some needed classes
import sun.net.*;
import java.net.*;
  1. Declare the port we send to
int port = 5000;
  1. Declare the "group." We use the InetAddress object but it's not really used as an Internet Address object; this just denotes a UDP listening group just an additional layer of isolation (in addition to the port). The address is just some arbitrary addresses (as long as it's not 255.255.255.255 as apparently some routers block this address for use with UDP)
String group = "225.4.5.6";
  1. Optional ttl
int ttl = 1;
  1. Create the socket but we don't bind it as we are only going to send data. Note that we don't have to join the multicast group if we are only sending data and not receiving.
MulticastSocket s = new MulticastSocket();
  1. Fill the buffer with some data
byte buf[] = byte[10];
for (int i = 0; i < buf.length; i++) buf[i] = (byte) i;
  1. Create the broadcast DatagramPacket
DatagramPacket pack = new DatagramPacket(buf, buf.length,
					 InetAddress.getByName(group), port);
  1. Send. Note that send takes a byte for the ttl and not an int.
s.send(pack, (byte) ttl);
  1. Clean up.
s.close();

The Receiver

  1. Import the classes
import sun.net.*;
import java.net.*;
  1. Declare the listening port (same as the sending port)
int port = 5000;
  1. Declare the "group." We use the InetAddress object but it's not really used as an Internet Address object; this just denotes a UDP listening group just an additional layer of isolation (in addition to the port) .
String group = "225.4.5.6";
  1. Create the socket and bind to the port
MulticastSocket s = new MulticastSocket(port);
  1. Create the DatagramPacket to block and receive
byte buf[] = byte[1024];
DatagramPacket pack = new DatagramPacket(buf, buf.length);
  1. Do something useful with the data
System.out.println("Received data from: " + pack.getAddress().toString() +
		    ":" + pack.getPort() + " with length: " +
		    pack.getLength());
System.out.write(pack.getData(), 0, pack.getLength());
System.out.println();
  1. Clean up
s.leaveGroup(InetAddress.getByName(group));
s.close();

Some Tips

  • if you find yourself having this Can't assign requested address java.net.SocketException (I encountered this error on OS X), try this solution
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment