You have to understand the following:
Broadcast is not Multicast.
Broadcast:
A broadcast is also on the ethernet a broadcast,
that means, the ethernet datagram has not a MAC address,
but a broadcast address.
Well, who cares? We do.
Because: The NIC forewards the datadram as packet to the TCP/IP layer,
(and there to the designated port... the process owning the port gets
the datagram)
So, nothing to join, no group no ip, just listen to a port.
Multicast:
Ok, this is on the TCP/IP layer alone.
A client has to JOIN a group (The group ranges from 224.0.0.0 to 239.255.255.255).
Here you do tell the router to foreward all packets sent to the group (and port).
So, this is absolutely diffrent to broadcast, but one single code line to add.
(exact would be to, as you have to leave the group)
The following code is a client, listening... of course has to be surrounded with try, catch etc.
//----------------CLIENT
int port = 44444;
MulticastSocket socket = new MulticastSocket(port);
InetAddress group = InetAddress.getByName(grp);
// if it is a multicast group, we do have to join the group
// (so that the router will send the muticast packets.
if (group.isMulticastAddress())
{
System.out.println("Group is an Multicast group, therefore joining group...");
socket.joinGroup(group);
}
else
{
System.out.println("Group is not a broadcast group, therefore ignoring group IP, just listening on port "+port+" for any datagram!");
socket.setBroadcast(true);
}
//
DatagramPacket packet;
byte[] buf = new byte[256];
packet = new DatagramPacket(buf, buf.length);
socket.receive(packet);
.
.
.
///at the end, you may add, or care previosly to remember this is a multicast join:
try{
socket.leaveGroup(group);
}
catch(Exception ex)
{
// dont care....
}
socket.close();
//--- The server is simplier:
int port = 44444;
socket = new DatagramSocket(this.iPortBroadcast);
while (socket.isBound())
{
try
{
ByteArrayOutputStream bout = new ByteArrayOutputStream();
bout.add(.......... // here you do have to something

InetAddress ipGroup = InetAddress.getByName("255.255.255.255");
DatagramPacket packet = new DatagramPacket(bout.toByteArray(), bout.size(), ipGroup, this.iPortBroadcast);
socket.send(packet);
}....