组播在发送者和接收者之间实现点对多点网络连接。如果发送者同时给多个接收者传输相同数据,只需将相同的这份数据包发送到组播组地址,然后由组播地址再传输给接收者。
java中的组播socket用java.net.MulticastSocekt类来完成,该类继承了DataGramSocket类。三个构造函数如下:
public MulticastSocket() throws SocketException
public MulticastSocket(int port) throws SocketException
public MulticastSocket(SocketAddress bindAddress) throws SocketException
与组播通信需要几个操作:加入组播组、接收数据、发送数据、离开组播组
public void joinGroup(InetAddress address)throws IOException
public void joinGroup(SocketAddress address,NetworkInterface interface)throws IOException //来自java1.4
public void leaveGroup(InetAddress address)throws IOException
public void leaveGroup(SocketAddress address,NetworkInterface interface)throws IOException //来自java1.4
public void send(DatagramPacket packet,byte ttl) throws IOException
public void setLoopbackMode(boolean disable)throws SocketException //java1.4