监听者
1 import java.io.IOException; 2 import java.net.InetAddress; 3 import java.net.InetSocketAddress; 4 import java.net.NetworkInterface; 5 import java.net.StandardProtocolFamily; 6 import java.net.StandardSocketOptions; 7 import java.nio.ByteBuffer; 8 import java.nio.channels.Channels; 9 import java.nio.channels.DatagramChannel; 10 import java.nio.channels.MulticastChannel; 11 import java.nio.channels.WritableByteChannel; 12 import java.util.Enumeration; 13 14 public class MulticastSniffer { 15 16 public static void main(String[] args) throws IOException { 17 //NetworkInterface interf = NetworkInterface.getByInetAddress(InetAddress.getLocalHost()); 18 NetworkInterface interf = NetworkInterface.getByInetAddress(InetAddress.getByName("192.168.1.181")); 19 //设置本地硬件端口; 20 21 InetSocketAddress group = new InetSocketAddress(InetAddress.getByName("224.0.0.1"), 2000); 22 //设置组播地址 23 DatagramChannel channel = DatagramChannel.open(StandardProtocolFamily.INET) 24 .setOption(StandardSocketOptions.SO_REUSEADDR, true) 25 .setOption(StandardSocketOptions.IP_MULTICAST_LOOP, false) 26 .bind(group) 27 .setOption(StandardSocketOptions.IP_MULTICAST_IF, interf); 28 29 channel.configureBlocking(true); 30 31 try(MulticastChannel multicast = channel){ 32 multicast.join(group.getAddress(), interf); 33 34 byte[] data = new byte[8192]; 35 ByteBuffer buffer = ByteBuffer.allocate(8192); 36 WritableByteChannel out = Channels.newChannel(System.out); 37 while((((DatagramChannel)multicast).receive(buffer))!= null) { 38 buffer.flip(); 39 out.write(buffer); 40 buffer.clear(); 41 } 42 43 }catch(IOException e) { 44 e.printStackTrace(); 45 } 46 47 } 48 49 }
发送者
1 import java.io.IOException; 2 import java.net.InetAddress; 3 import java.net.InetSocketAddress; 4 import java.net.NetworkInterface; 5 import java.net.StandardProtocolFamily; 6 import java.net.StandardSocketOptions; 7 import java.nio.ByteBuffer; 8 import java.nio.channels.DatagramChannel; 9 import java.nio.channels.MulticastChannel; 10 11 public class MulticastSender { 12 13 public static void main(String[] args) throws IOException { 14 15 // NetworkInterface interf = NetworkInterface.getByInetAddress(InetAddress.getLocalHost()); 16 17 NetworkInterface interf = NetworkInterface.getByInetAddress(InetAddress.getByName("192.168.1.181")); 18 //设置本地硬件端口; 19 20 InetSocketAddress group = new InetSocketAddress(InetAddress.getByName("224.0.0.1"), 2000); 21 //设置组播地址 22 DatagramChannel channel = DatagramChannel.open(StandardProtocolFamily.INET) 23 .setOption(StandardSocketOptions.SO_REUSEADDR, true) 24 .bind(group) 25 .setOption(StandardSocketOptions.IP_MULTICAST_IF, interf); 26 27 //.setOption(StandardSocketOptions.IP_MULTICAST_LOOP, false) 28 channel.configureBlocking(true); 29 30 try(MulticastChannel multicast = channel){ 31 multicast.join(group.getAddress(), interf); 32 33 ByteBuffer buffer = ByteBuffer.allocate(8192); 34 buffer.put((InetAddress.getLocalHost().toString()+‘\n‘).getBytes()); 35 for(int i=0; i<3; i++) { 36 buffer.flip(); 37 ((DatagramChannel)multicast).send(buffer, group); 38 } 39 40 }catch(IOException e) { 41 e.printStackTrace(); 42 } 43 44 } 45 46 }
原文地址:https://www.cnblogs.com/WALLACE-S-BOOK/p/9734462.html
时间: 2024-10-18 20:07:36