How to use Java MulicastSocket (UDP)?


I develop client-server application, working in real-time. Server and clients exchanges by small messages, so I choose UDP for my architecture (as suggested in many articles in network). It's not a problem for me to use default java's DatagramSocket/DatagramPacket for orginizng all things as I want, but when I read documentation I see the "MulticastSocket" opportunity. But it is completely unclear for me: How MutlicastSocket at the user side will know where to connect? (public IP/port of the server). Really, as this shown at this official java tutorial. MulticastSocket creates like:

MulticastSocket socket = new MulticastSocket(4446);
InetAddress group = InetAddress.getByName("");

and there is NO any specification about public server IP and port. What is ""? It is possibles that tones of applications send something to that address in web, isn't it?

When I create client in regular (not Multicast) way I use something like:

DatagramSocket outputClientSocket = new DatagramSocket();
DatagramPacket outputPacket = new DatagramPacket(new byte[512],512,InetAddress.getByName("94.***.89.***"),9898);

where "94.???.89.???" is my server's public IP address, and 9898 is port of my server, that listens it. Like that:

DatagramSocket serverInputSocket = new DatagramSocket(9898);
DatagramPacket inputServerPacket = new DatagramPacket(new byte[512],512);

and after recieving something I can establish connection with client, and answer something for him, like that:

DatagramSocket socketForSpecificClient = new DatagramSocket();
InetAddress realClientAddress = inputServerPacket.getAddress();
int realClientPort = inputServerPacket.getPort();
DatagramPacket packetForSpecificClient = new DatagramPacket(new byte[512],512,realClientAddress,realClientPort);

This approach works well, even if client has no public IP. It is absolutely clear way of establishing connection for me, but I can't understand for what purposes MulticastSocket should be used?


Quoting the paragraphs from the book Java Network Programming for framing the answer :

How MutlicastSocket at the user side will know where to connect? (public IP/port of the server)?

What is ""? It is possibles that tones of applications send something to that address in web, isn't it?

Firstly, you should generally use IP-Address lying between to for creating a new multicast group.

When a host wants to send data to a multicast group, it puts that data in multicast datagrams, which are nothing more than UDP datagrams addressed to a multicast group. Multicast data is sent via UDP.

A multicast address is the shared address of a group of hosts called a multicast group. IPv4 multicast addresses are IP addresses in the CIDR group (i.e., they range from to

A multicast group is a set of Internet hosts that share a multicast address. Any data sent to the multicast address is relayed to all the members of the group. Membership in a multicast group is open; hosts can enter or leave the group at any time. Groups can be either permanent or transient. The IANA is responsible for handing out permanent multicast addresses as needed.

Multicasting sends data from one host to many different hosts, but not to everyone; the data only goes to clients that have expressed an interest by joining a particular multicast group. In a way, this is like a public meeting. People can come and go as they please, leaving when the discussion no longer interests them. Before they arrive and after they have left, they don’t need to process the information at all: it just doesn’t reach them. On the Internet, such “public meetings” are best implemented using a multicast socket that sends a copy of the data to a location (or a group of locations) close to the parties that have declared an interest in the data.

In the best case, the data is duplicated only when it reaches the local network serving the interested clients: the data crosses the Internet only once. More realistically, several identical copies of the data traverse the Internet; but, by carefully choosing the points at which the streams are duplicated, the load on the network is minimized. The good news is that programmers and network administrators aren’t responsible for choosing the points where the data is duplicated or even for sending multiple copies; the Internet’s routers handle all that.

To receive data that is being multicast from a remote site, first create a MulticastSocket with the MulticastSocket() constructor. As with other kinds of sockets, you need to know the port to listen on. This code fragment opens a MulticastSocket that listens on port 2300:

MulticastSocket ms = new MulticastSocket(2300);

Next, join a multicast group using the MulticastSocket ’s joinGroup() method:

InetAddress group = InetAddress.getByName("");

This signals the routers in the path between you and the server to start sending data your way and tells the local host that it should pass you IP packets addressed to the multicast group. Once you’ve joined the multicast group, you receive UDP data just as you would with a DatagramSocket .

It is possibles that tones of applications send something to that address in web, isn't it?

One would face the same problem even in case of unicast communication if the address is known on which the service is provided.

Multicast is in IPv4 usually not working across network segments. If your application is supposed to work on the internet (and not e.g. just within an intranet under your control), you can not base your communcation on multicast.

Edit: Here are some further resources on the subject:

Wikipedia on IP multicast:

multicast services are generally not available to the average end-user

Other Stackoverflow question 'UDP Multicast over the internet?':

In general this is not possible since multicast packages aren't routed.

Discussion on 'Does multicast work over the itnernet(sic)?':

ISPs filter mutlicast you can't join a multicast stream over the internet.

These are just a few of the first hits when googling for 'using multicast over the internet'.

The address range is reserved for use in 'documentation and example code' so the address in the example,, does not point to a real endpoint.

If you need a real, public multicast address and are connected through an ISP with multicast support, you have to obtain one from the IANA registry. You are right that anyone can send (potentially bogus) data to that IP address, but you have exactly the same problem with unicast addresses. If you provide a service on a unicast address, anyone can connect to that address and send data to it.

By : jarnbjo

This video can help you solving your question :)
By: admin