Today reviewed the Java Network programming this aspect (actually did not learn before), before Linux under the C to do some examples, but not for a long time no use also forget all. I don't know what C + + is not very good to remember, or did not learn well.
In the case of multicast, in fact, no other problem, "Java Network Programming" This book is very clear, but encountered one of the most puzzling API, as follows:
Joingroup in Java multicastsocket (socketaddress mcastaddr,networkinterface netif)
Why is the first parameter a socketaddress instead of a inetaddress ah, the port number in socketaddress is used for hair? Unfortunately, the API does not explain why the socketaddress, and then had to do the experiment automatically.
Import java.util.*;import java.io.*;import java.net.*;p ublic class Server {public static void main (string[] args) throws E Xception{inetaddress group=inetaddress.getbyname ("224.2.2.2"); MulticastSocket ms=new MulticastSocket (8989); Ms.joingroup (new Inetsocketaddress (group,1000), Networkinterface.getbyname ("172.31.164.11")); byte[] Buffer=new byte[8192];D atagrampacket dp=new datagrampacket ( Buffer,buffer.length); while (true) {ms.receive (DP); byte[] Data=dp.getdata (); String Line=new string (Data,dp.getoffset (), dp.getlength ()); System.out.println (line);dp. SetLength (Buffer.length);}}
The client's code is not given (too simple). If run as above, the server can only receive multicast data on port 8989, and cannot receive multicast data sent to Port 1000.
With Netstat-ano | Findstr also did not find a port number 1000th on the monitor.
SocketAddress in So.....joingroup (socketaddress mcastaddr,networkinterface netif) is only used in the InetAddress section.
About Joingroup in Java multicastsocket (socketaddress mcastaddr,networkinterface netif)