Question

I need to multicast a message across connected clients but facing a problem. I have tried the following snippet for this link:

String msg = "Hello";
InetAddress group = InetAddress.getByName("228.5.6.7");
MulticastSocket s = new MulticastSocket(6789);
s.joinGroup(group);
DatagramPacket hi = new DatagramPacket(msg.getBytes(), msg.length(),
                         group, 6789);
s.send(hi);

I am getting an exception:

java.net.SocketException: Not a multicast address

I tried:

  • localhost
  • 127.0.0.1
  • 192.168.1.29(my local ip)

What could have gone wrong. Being new to this topic i am unable to debug it. Thanks for any help.

Was it helpful?

Solution

Your code works for me.

You don't need to join a group to send to it. Only to receive from it. However if you do join it, you need to specify an IP address that is a valid multicast address. Despite what it says in the code you posted, clearly your actual code doesn't use a valid multicast address.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top