I'm working in client server project in java. i'm using MulticastSocket . I have to send some message to selective clients. But i don't know how to get joined client address. Can anyone please help me out.
Asked
Active
Viewed 131 times
1 Answers
0
Use code like below, this may help you.
private void init() throws IOException {
DatagramChannel channel = DatagramChannel.open(StandardProtocolFamily.INET);
channel.setOption(StandardSocketOptions.SO_REUSEADDR, true);
channel.configureBlocking(true); //optional
channel.bind(new InetSocketAddress(5000));
InetAddress iGroup = InetAddress.getByName("224.0.0.1");
NetworkInterface intrf = NetworkInterface.getByName("lo"); // lo name could be changed according your requirement
channel.setOption(StandardSocketOptions.IP_MULTICAST_IF, intrf);
channel.join(iGroup, intrf);
}

Pandey Amit
- 657
- 6
- 19
-
I think above code is restrict client address to join `DatagramChannel`. But in my scenario, no restriction for clients to join. Any number of client can join, they will do some other operations. For example, if 10 client joined in my network, i have to send message to selective 5 clients. – Madhan Kumar Oct 05 '18 at 09:20
-
If you have multiple clients, in this case, you can maintain a list of client socket to communicate individually – Pandey Amit Oct 05 '18 at 09:50
-
ya right. But i' using `MulticastSocket`, here how can i get the joined client address. When i use `MulticastSocket`, can't able to get the client details. But it will send a message to all clients. Here is my code to send message to all clients `String s = txtin.getText(); DatagramPacket p = new DatagramPacket(s.getBytes(), s.length(), group, 4444); msoc.send(p);` – Madhan Kumar Oct 05 '18 at 10:45