Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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("203.0.113.0");
socket.joinGroup(group);

and there is NO any specification about public server IP and port. What is "203.0.113.0"? 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);
serverInputSocket.recieve(inputServerPacket);

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);
socketForSpecificClient.send(packetForSpecificClient);

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?

like image 947
Crabonog Avatar asked Jan 29 '26 11:01

Crabonog


1 Answers

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 hardforum.com '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 203.0.113.0/24 is reserved for use in 'documentation and example code' so the address in the example, 203.0.113.0, 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.

like image 56
jarnbjo Avatar answered Feb 01 '26 01:02

jarnbjo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!