首页 » Program » Java » 正文

Java multicast

MulticastSocketServer.java

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;

public class MulticastSocketServer {
      public static void main(String[] args) throws Exception {
          InetAddress group = InetAddress.getByName("224.0.0.4");   //组播地址  
          int port = 9876; // 端口  
          MulticastSocket mss = null; 
          try {  
                mss = new MulticastSocket(port);   // \1.创建一个用于发送和接收的MulticastSocket组播套接字对象  
                mss.joinGroup(group);   //3.使用组播套接字joinGroup(),将其加入到一个组播  
                
                System.out.println("begining send packet ---" + new java.util.Date() + ")");  
                while (true) {  
                    String message = "Hello" + new java.util.Date();   
                    byte[] buffer = message.getBytes();
                    DatagramPacket dp = new DatagramPacket(buffer, buffer.length, group, port);   //2.创建一个指定缓冲区大小及组播地址和端口的DatagramPacket组播数据包对象 
                    mss.send(dp);   //4.使用组播套接字的send()方法,将组播数据包对象放入其中,发送组播数据包  
                    // String s = new String(dp.getData(), 0, dp.getLength()); //5.解码组播数据包提取信息,并依据得到的信息作出响应  
                    System.out.println("发送数据包给" + group + ":" + port);  
                    Thread.sleep(1000);  
                }  
            } catch (IOException e) {  
                e.printStackTrace();  
      
            } finally{  
                try {  
                    if(mss!=null){  
                        mss.leaveGroup(group);  
                        mss.close();  
                    }  
                } catch (Exception e2) {  
                    // TODO: handle exception  
                }  
            }  
      }
}

MulticastSocketClient.java:

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;

public class MulticastSocketClient {
    public static void main(String[] args) throws Exception {
        InetAddress group = InetAddress.getByName("224.0.0.4");   //组播地址
        int port = 9876;   //端口
        MulticastSocket msr = null;
        try {  
            msr = new MulticastSocket(port);   //1.创建一个用于发送和接收的MulticastSocket组播套接字对象  
            msr.joinGroup(group);   //3.使用组播套接字joinGroup(),将其加入到一个组播  
            byte[] buffer = new byte[2048];   //设置接受数据的缓冲区大小  
            System.out.println("Begining get packet ---" + new java.util.Date() + ")");  
            while (true) {  
                DatagramPacket dp = new DatagramPacket(buffer, buffer.length);   //2.创建一个指定缓冲区大小及组播地址和端口的DatagramPacket组播数据包对象  
                msr.receive(dp);   //4.使用组播套接字的receive()方法,将组播数据包对象放入其中,接收组播数据包  
                String s = new String(dp.getData(), 0, dp.getLength());   //5.解码组播数据包提取信息,并依据得到的信息作出响应  
                System.out.println(s);  
            }  
        } catch (IOException e) {  
            e.printStackTrace();  
  
        }finally {  
            if (msr != null) {  
                try {  
                    msr.leaveGroup(group);   //7.使用组播套接字的leaveGroup()方法,离开组播组  
                    msr.close();   //关闭组播套接字  
                } catch (IOException e) {  
                }  
            }  
        }  
    }
}

Comment