1

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.

kleopatra
  • 51,061
  • 28
  • 99
  • 211
Nitesh Verma
  • 1,795
  • 4
  • 27
  • 46

1 Answers1

1

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.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • it worked now but it works only between two connections only. Is it possible to make it work for one to many connections? – Nitesh Verma Jul 19 '13 at 06:35
  • Eh? Multicast *is* one to many. You can have as many group members as you like. All they have to do is join the group and receive. – user207421 Jul 19 '13 at 22:53