Java Reference
In-Depth Information
public static void main(String[] args) {
MulticastServer server = new MulticastServer();
server.start();
}
@Override
public void run() {
try {
// send the response to the client at
"address" and "port"
InetAddress address
= InetAddress.getByName("226.18.84.25");
int port = 5239;
DatagramChannel server
= DatagramChannel.open().bind(null);
System.out.println("Sending datagram packet
to group " + address + " on port " + port);
message = ByteBuffer.wrap("Hello to all
listeners".getBytes());
server.send(message, new
InetSocketAddress(address, port));
server.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
}
The server can broadcast a message to each client that is a member of the group.
The client should be initiated first, followed by the server. Once the server is started, it
will broadcast the message, and the client will receive it.