MulticastSocketServer.java

 package cn.edu.buaa.multicast;

 import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.UnknownHostException; /**
* One thing that we need to take into consideration here, is that there are
* specific addresses that allow us to use a MulticastSocket are limited,
* specifically in the range of 224.0.0.0 to 239.255.255.255. Some of them are
* reserved, like 224.0.0.0. The address that we are using, 224.0.0.3, can be
* used safely.
*/
public class MulticastSocketServer { final static String INET_ADDR = "224.0.0.3";
final static int PORT = 8888; public static void main(String[] args) throws UnknownHostException, InterruptedException {
// Get the address that we are going to connect to.
InetAddress addr = InetAddress.getByName(INET_ADDR); // Open a new DatagramSocket, which will be used to send the data.
try (DatagramSocket serverSocket = new DatagramSocket()) {
for (int i = 0; i < 5; i++) {
String msg = "Sent message no " + i; // Create a packet that will contain the data
// (in the form of bytes) and send it.
DatagramPacket msgPacket = new DatagramPacket(msg.getBytes(), msg.getBytes().length, addr, PORT);
serverSocket.send(msgPacket); System.out.println("Server sent packet with msg: " + msg);
Thread.sleep(500);
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}

MulticastSocketClient.java

 package cn.edu.buaa.multicast;

 import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.net.UnknownHostException; public class MulticastSocketClient {
final static String INET_ADDR = "224.0.0.3";
final static int PORT = 8888; public static void main(String[] args) throws UnknownHostException {
// Get the address that we are going to connect to.
InetAddress address = InetAddress.getByName(INET_ADDR); // Create a buffer of bytes, which will be used to store
// the incoming bytes containing the information from the server.
// Since the message is small here, 256 bytes should be enough.
byte[] buf = new byte[256]; // Create a new Multicast socket (that will allow other sockets/programs
// to join it as well.
try (MulticastSocket clientSocket = new MulticastSocket(PORT)) {
// Joint the Multicast group.
clientSocket.joinGroup(address); while (true) {
// Receive the information and print it.
DatagramPacket msgPacket = new DatagramPacket(buf, buf.length);
clientSocket.receive(msgPacket); String msg = new String(buf, 0, buf.length);
System.out.println("Socket 1 received msg: " + msg);
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}

Java 组播的更多相关文章

  1. java组播MulticastSocket

    在单播模式中有服务器端和客户端之分,而组播模式与单播模式不同,每个端都是以路由器或交换机做为中转广播站,任意一端向路由器或交换机发送消息,路由或交换机负责发送其他节点,每个节点都是同等的.所以在编程模 ...

  2. 组播(Multicast)传输

    组播(Multicast)传输: 在发送者和每一接收者之间实现点对多点网络连接. 如果一台发送者同时给多个的接收者传输相同的数据,也只需复制一份的相同数据包.它提高了数据传送效率.减少了骨干网络出现拥 ...

  3. Android开发:组播(多播)与广播

    近期由于需要编写能够使同一局域网中的Android客户端与PC端进行自动匹配通信功能的程序,学习并试验了JAVA组播与广播的内容,记录一些理解如下: 一.组播(多播) 背景知识:组播使用UDP对一定范 ...

  4. Java单播、广播、多播(组播)---转

    一.通信方式分类 在当前的网络通信中有三种通信模式:单播.广播和多播(组播),其中多播出现时间最晚,同时具备单播和广播的优点. 单播:单台主机与单台主机之间的通信 广播:当台主机与网络中的所有主机通信 ...

  5. Java单播、广播、多播(组播)

    一.通信方式分类 在当前的网络通信中有三种通信模式:单播.广播和多播(组播),其中多播出现时间最晚,同时具备单播和广播的优点. 单播:单台主机与单台主机之间的通信 广播:当台主机与网络中的所有主机通信 ...

  6. netty的Udp单播、组播、广播实例+Java的Udp单播、组播、广播实例

    网络上缺乏netty的udp的单播.组播案例,经过一番学习总结之后终于把这两个案例调通,下面把这两个案例的代码放在这里分享一下. 首先推荐博文: http://colobu.com/2014/10/2 ...

  7. Java单播、组播(多播)、广播的简单实现

    简介 单播有TCP和UDP两种实现,组播(多播)和广播只有UDP一种实现.单播和广播基本一样,只是广播的数据包IP为广播IP.   单播 DatagramSocket和DatagramPacket 服 ...

  8. Android MulticastSocket IP组播

    MulticastSocket是对等的关系,也就是他们通过IP多点组播保留的地址来相互接收在这个组的Socket发送的消息 我写了一个例子,MultiSocketB是一个service,用于先接收再发 ...

  9. Android为TV端助力:UDP协议(接收组播和单播)

    private static String MulticastHost="224.9.9.98";private static int POST=19999;private sta ...

随机推荐

  1. JS跳出框架返回上一页

    链接部分 <a class="link" href="javascript:;" target="_top" onclick=&quo ...

  2. bzoj1051

    就是一个tarjan #include<iostream> #include<stack> #include<cstdio> using namespace std ...

  3. bzoj1787

    lca裸题,画画图看看就可以了,找出那个一次公共祖先,求距离 #include<iostream> #include<set> #include<cstring> ...

  4. hibernate在使用sql查询query自动转化成model类型数据,query.addEntity

    hibernate使用自动的hql查询或者其封装的查询方法都能字段转化成对象 而如果在hibernate中使用sql时大多返回为Object[]对象 那么如何将object[]转换成model呢,答案 ...

  5. 使用iframe 或frameset框架退出不成功

    iframe .frameset框架有时登录界面加载在框架当中,但我们想直接到登录界面,这时我们可以使用js来实现.if(window!=top) { top.location.href=locati ...

  6. 在MAC上搭建tomcat,再使用servlet时遇到的问题。

    说起来真是惭愧.在mac上配置tomcat环境时.tomcat6能正确运行.但是7,8都运行不了.具体表现是tomcat6访问127.0.0.1:8080可以显示那个界面,然而tomcat7和8都显示 ...

  7. js-JavaScript高级程序设计学习笔记12

    第十五章 使用canvas绘图 1.要使用<canvas>元素,必须先设置其width和height属性. 2.要在这块画布上绘图,需要先取得绘图上下文,取得绘图上下文对象的引用,需要调用 ...

  8. 【BZOJ-4568】幸运数字 树链剖分 + 线性基合并

    4568: [Scoi2016]幸运数字 Time Limit: 60 Sec  Memory Limit: 256 MBSubmit: 238  Solved: 113[Submit][Status ...

  9. 【BZOJ-1009】GT考试 KMP+DP+矩阵乘法+快速幂

    1009: [HNOI2008]GT考试 Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 2745  Solved: 1694[Submit][Statu ...

  10. asp.net下webform的ReadOnly和Enabled属性最终渲染的结果

    ReadOnly对应readonly="readonly" Enabled对应disabled="disabled" 然后研究了一下这两种的用法,特此标记一下: ...