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:
What could have gone wrong. Being new to this topic i am unable to debug it. Thanks for any help.
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.