IP组播 MulticastChannel接口 DatagramChannel实现
监听者
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.NetworkInterface;
import java.net.StandardProtocolFamily;
import java.net.StandardSocketOptions;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.DatagramChannel;
import java.nio.channels.MulticastChannel;
import java.nio.channels.WritableByteChannel;
import java.util.Enumeration; public class MulticastSniffer { public static void main(String[] args) throws IOException {
//NetworkInterface interf = NetworkInterface.getByInetAddress(InetAddress.getLocalHost());
NetworkInterface interf = NetworkInterface.getByInetAddress(InetAddress.getByName("192.168.1.181"));
//设置本地硬件端口; InetSocketAddress group = new InetSocketAddress(InetAddress.getByName("224.0.0.1"), 2000);
//设置组播地址
DatagramChannel channel = DatagramChannel.open(StandardProtocolFamily.INET)
.setOption(StandardSocketOptions.SO_REUSEADDR, true)
.setOption(StandardSocketOptions.IP_MULTICAST_LOOP, false)
.bind(group)
.setOption(StandardSocketOptions.IP_MULTICAST_IF, interf); channel.configureBlocking(true); try(MulticastChannel multicast = channel){
multicast.join(group.getAddress(), interf); byte[] data = new byte[8192];
ByteBuffer buffer = ByteBuffer.allocate(8192);
WritableByteChannel out = Channels.newChannel(System.out);
while((((DatagramChannel)multicast).receive(buffer))!= null) {
buffer.flip();
out.write(buffer);
buffer.clear();
} }catch(IOException e) {
e.printStackTrace();
} } }
发送者
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.NetworkInterface;
import java.net.StandardProtocolFamily;
import java.net.StandardSocketOptions;
import java.nio.ByteBuffer;
import java.nio.channels.DatagramChannel;
import java.nio.channels.MulticastChannel; public class MulticastSender { public static void main(String[] args) throws IOException { // NetworkInterface interf = NetworkInterface.getByInetAddress(InetAddress.getLocalHost()); NetworkInterface interf = NetworkInterface.getByInetAddress(InetAddress.getByName("192.168.1.181"));
//设置本地硬件端口; InetSocketAddress group = new InetSocketAddress(InetAddress.getByName("224.0.0.1"), 2000);
//设置组播地址
DatagramChannel channel = DatagramChannel.open(StandardProtocolFamily.INET)
.setOption(StandardSocketOptions.SO_REUSEADDR, true)
.bind(group)
.setOption(StandardSocketOptions.IP_MULTICAST_IF, interf); //.setOption(StandardSocketOptions.IP_MULTICAST_LOOP, false)
channel.configureBlocking(true); try(MulticastChannel multicast = channel){
multicast.join(group.getAddress(), interf); ByteBuffer buffer = ByteBuffer.allocate(8192);
buffer.put((InetAddress.getLocalHost().toString()+'\n').getBytes());
for(int i=0; i<3; i++) {
buffer.flip();
((DatagramChannel)multicast).send(buffer, group);
} }catch(IOException e) {
e.printStackTrace();
} } }
IP组播 MulticastChannel接口 DatagramChannel实现的更多相关文章
- IP组播技术介绍及实现例子
引 言 近年来,随着Internet的迅速普及和爆炸性发展,在Internet上产生了许多新的应用,其中不少是高带宽的多媒体应用,譬如网 络视频会议.网络音频/视频广播.AOD/VOD.股市行情发布. ...
- IP组播
1 IP组播基础 IP组播技术有效地解决了单点发送.多点接收的问题.组播源只发送一份数据,被传递的信息在距组播源尽可能远的网络节点才开始被复制和分发,并且只发送给需要该信息的接收者. 说明: 本章 ...
- IP组播技术
1 概述 1.1 产生背景 传统的IP通信有两种方式:一种是在源主机与目的主机之间点对点的通信,即单播:另一种是在源主机与同一网段中所有其它主机之间点对多点的通信,即广播.如果要将信息发送给多 ...
- 【知识强化】第四章 网络层 4.7 IP组播
这节课我们来学习一下IP组播. 首先我们来看这样一个问题,IP数据报在网络当中传输的时候,有几种传输方式呢?三种,分别是单播.广播和组播(多播).这个组播呢也叫做多播,它们俩是一个意思.那这个组播是由 ...
- 计算机网络之网络层IP组播(IGMP、组播路由选择协议、组播地址)
文章转自:https://blog.csdn.net/weixin_43914604/article/details/105318560 学习课程:<2019王道考研计算机网络> 学习目的 ...
- Android MulticastSocket IP组播
MulticastSocket是对等的关系,也就是他们通过IP多点组播保留的地址来相互接收在这个组的Socket发送的消息 我写了一个例子,MultiSocketB是一个service,用于先接收再发 ...
- TCP/IP 笔记 - 广播和本地组播
在之前第二章介绍IP寻址的时候有介绍到,IP地址有4种:单播.组播.广播.任播. 单播,客户端与服务器之间点到点连接通信: 组播,在发送者和多个接收者(如某个特定的分组)之间实现点对多点的连接通信: ...
- 《TCP/IP 详解 卷1:协议》第 9 章:广播和本地组播(IGMP 和 MLD)
我已经懒了,卷一已经是去年年底看完的,但怎么说卷一的坑开了就要填完啊-- 广播和本地组播(IGMP 和 MLD) 引言 有 4 种 IP 地址,单播(unicast).任播(anycast).组播(m ...
- D类IP地址和组播传输
在224.0.0.0-239.255.255.255范围内的地址称为D类IP组播地址.其中,224.0.0.0-224.0.0.255为预留的组播地址(永久组地址),地址224.0.0.0保留不做分配 ...
随机推荐
- oracle sql练习 菜鸟入门!
进入公司 ,首先是进行SQL培训 一下是针对oracle的emp与dept表进行的基础查询 --1.选择部门30中的所有员工: ; --2.列出所有办事员(CLERK)的姓名,编号和部门编号: sel ...
- Attempt to load Oracle client libraries threw BadImageFormatException. This problem will occur when running in 64 bit mode with the 32 bit Oracle client components installed.
System.Data.OracleClient 已经过时了.微软不再支持它. 因此,我建议你为. NET 使用Oracle数据提供程序:ODP.Net. 你可以从以下位置下载: 版本:Release ...
- [C++]auto_ptr绑定到指针
接受指针的构造函数为explicit构造函数,所以必须使用初始化的直接形式来创建auto_ptr对象: auto_ptr<int> pi = new int(1024);//error a ...
- java一些使用
随机数.输入.byte数组和string转换 一些可能会使用到的方法.供及时查找 ########################random类使用 Random random = new Rando ...
- Io性能分析
一.iostat使用说明 1.命令使用方法 使用ixstat –x 1 可以每隔1秒钟采集所有设备的io信息.其中的1类似于使用“vmstat 1”后面的1. 2.命令格式说明 ―――――――――― ...
- xgcom linux下的串口助手
好用到爆@@! 2.Install: Source code: http://code.google.com/p/xgcom/ svn checkout http://xgcom.googlecode ...
- codeforces A. Supercentral Point 题解
版权声明:本文作者靖心,靖空间地址:http://blog.csdn.net/kenden23/,未经本作者同意不得转载. https://blog.csdn.net/kenden23/article ...
- classlist和array.prototype.slice.call
1.classlist document.getElementById("myDIV").classList.add("mystyle"); classList ...
- nginx发布antd-pro项目(别人发的,未测试)
server { listen ; server_name localhost; #charset koi8-r; charset utf-; #access_log logs/host.access ...
- Blocking Master Example QT 自带 的 serial 即 串口 例子
1.官方解释文档:http://doc.qt.io/qt-5/qtserialport-blockingmaster-example.html Blocking Master shows how to ...