欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

IP组播 MulticastChannel接口 DatagramChannel实现

程序员文章站 2022-07-10 13:43:39
监听者 发送者 ......

监听者

 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 }