1.UDP广播。
客户端开启一个端口监听数据,服务器端指定该端口发送数据,客户端即可收到数据,服务器绑定的广播地址为255.255.255.255
客户端监听:
public class BroadcastClient {
private int MessageIndex = 0;
private int port = 6787;//数据监听绑定端口
private boolean closed = false;
public void start() {
new Thread(new Runnable() {
@Override
public void run() {
System.out.println("BroadcastClient start ");
runClient();
}
}).start();
}
DatagramSocket socket = null;
private void runClient() {
try {
byte[] receiveBuffer = new byte[1024];//数据缓冲区
socket = new DatagramSocket(port);//绑定端口进行数据监听
DatagramPacket receivePacket = new DatagramPacket(receiveBuffer, receiveBuffer.length);//数据接收包囊
while (!closed) {
count();//计算数量,超过五十次关闭连接
socket.receive(receivePacket);//接收数据
System.out.println("received packet from " + receivePacket.getAddress().getHostAddress() + " : " + receivePacket.getPort());
String msg = new String(receivePacket.getData(), receivePacket.getOffset(), receivePacket.getLength());
System.out.println("received " + msg);
Thread.sleep(2000);
}
System.out.println("socket close ");
socket.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void count() {
MessageIndex++;
if (MessageIndex >= 50) {
closed = true;
}
}
public static void main(String[] s) {
BroadcastClient client = new BroadcastClient();
client.start();
}
}
//运行效果
BroadcastClient start
received packet from 192.168.2.221 : 58458
received hello ,message from server,0
received packet from 192.168.2.221 : 58460
received hello ,message from server,1
received packet from 192.168.2.221 : 58462
received hello ,message from server,2
received packet from 192.168.2.221 : 58464
服务端发送数据:
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class UdpBroadcastServer {
public boolean closed = false;
public String ip = "255.255.255.255";//广播地址
public int port = 6787;//指定广播接收数据端口
public int MessageIndex = 0;//数据发送次数
public void start(){
new Thread(new Runnable() {
@Override
public void run() {
System.out.println("UdpBroadcastServer start ");
runServer();
}
}).start();
}
private void runServer(){
try {
while(!closed){
Thread.sleep(2000);
send();
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("UdpTestServer run Exception: "+e.toString());
}
}
public void send(){
try{
String sendMessage="hello ,message from server,"+MessageIndex++;
InetAddress adds = InetAddress.getByName(ip);
DatagramSocket ds = new DatagramSocket();
DatagramPacket dp = new DatagramPacket(sendMessage.getBytes(),sendMessage.length(), adds, port);
ds.send(dp);
ds.close();
}catch (Exception e) {
System.out.println("UdpTestServer send Exception: "+e.toString());
}
if(MessageIndex>=50){
closed = true;
System.out.println("closed ");
}
}
/**
* @param args
*/
public static void main(String[] args) {
UdpBroadcastServer server = new UdpBroadcastServer();
server.start();
}
}
//运行效果
UdpBroadcastServer start
closed
2.UDP组播
组播需要加入一个组播地址,需要特定端口来接收数据
1.加入的组播ip不一样,是不能收到组播的
2.加入的组播ip一样,组播port不一样,是不能收到组播的
3.加入的组播ip一样,组播port一样,可以收到发送的组播数据,就算自己发送的数据也能收到
客户端:
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.MulticastSocket;
/**
* 组播需要加入一个组播地址,需要特定端口来接收数据
* 1.加入的组播ip不一样,是不能收到组播的
* 2.加入的组播ip一样,组播port不一样,是不能收到组播的
* 3.加入的组播ip一样,组播port一样,可以收到发送的组播数据,就算自己发送的数据也能收到
*/
public class UpdGroupClient {
private int MessageIndex = 0;
private String ip = "224.0.0.1";//组播地址
private int port = 6789;//指定数据接收端口
private boolean closed = false;
public void start() {
new Thread(new Runnable() {
@Override
public void run() {
System.out.println("UpdGroupClient start ");
runClient();
}
}).start();
}
MulticastSocket socket = null;
public void runClient() {
try {
byte[] receiveBuffer = new byte[1024];
DatagramPacket receivePacket = new DatagramPacket(receiveBuffer, receiveBuffer.length);
InetAddress group = InetAddress.getByName(ip);
socket = new MulticastSocket(port);
socket.joinGroup(group);//加入组播地址
while (!closed) {
send();
socket.receive(receivePacket);
System.out.println("received packet from " + receivePacket.getAddress().getHostAddress() + " : " + receivePacket.getPort());
String msg = new String(receivePacket.getData(), receivePacket.getOffset(), receivePacket.getLength());
System.out.println("received " + msg);
Thread.sleep(2000);
}
socket.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 组播发送数据
*/
public void send() {
String msg = "message from client," + MessageIndex++;
try {
byte[] message = msg.getBytes(); //发送信息
InetAddress inetAddress = InetAddress.getByName(ip); //指定组播地址
DatagramPacket datagramPacket = new DatagramPacket(message, message.length, inetAddress, port); //发送数据包囊
MulticastSocket multicastSocket = new MulticastSocket();//创建组播socket
multicastSocket.send(datagramPacket);
} catch (Exception e) {
e.printStackTrace();
}
if (MessageIndex >= 50) {
closed = true;
}
}
public static void main(String[] s) {
UpdGroupClient client = new UpdGroupClient();
client.start();
}
}
//运行效果
UpdGroupClient start
UpdGroupClient start
received packet from 192.168.2.221 : 57242
received message from client,0
received packet from 192.168.2.221 : 57244
received message from client,1
received packet from 192.168.2.221 : 57245
received hello ,message from server,0
received packet from 192.168.2.221 : 57247
received message from client,2
received packet from 192.168.2.221 : 57248
received hello ,message from server,1
received packet from 192.168.2.221 : 57249
received message from client,3
received packet from 192.168.2.221 : 57250
received hello ,message from server,2
received packet from 192.168.2.221 : 51521
received message from client,4
received packet from 192.168.2.221 : 51522
received hello ,message from server,3
received packet from 192.168.2.221 : 51523
received message from client,5
received packet from 192.168.2.221 : 51524
received hello ,message from server,4
received packet from 192.168.2.221 : 63331
received message from client,6
received packet from 192.168.2.221 : 63332
received hello ,message from server,5
received packet from 192.168.2.221 : 63333
服务器端:
public class UdpGroupServer {
public boolean closed = false;
public String ip = "224.0.0.1";//组播虚拟地址
public int port = 6789;//组播Ip
public int MessageIndex = 0;
public void start(){
new Thread(new Runnable() {
@Override
public void run() {
System.out.println("UdpTestServer start ");
runServer();
}
}).start();
}
private void runServer(){
try {
InetAddress group = InetAddress.getByName(ip);
MulticastSocket s = new MulticastSocket(port);
byte[] arb = new byte[1024];
s.joinGroup(group);//加入该组
while(!closed){
DatagramPacket datagramPacket =new DatagramPacket(arb,arb.length);
s.receive(datagramPacket);
System.out.println("received packet from " + datagramPacket.getAddress().getHostAddress() + " : " + datagramPacket.getPort());
System.out.println(new String(arb));
Thread.sleep(2000);
send();
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("UdpTestServer run Exception: "+e.toString());
}
}
public void send(){
try{
String sendMessage="hello ,message from server,"+MessageIndex++;
byte[] message = sendMessage.getBytes(); //发送信息
InetAddress inetAddress = InetAddress.getByName(ip); //指定组播地址
DatagramPacket datagramPacket = new DatagramPacket(message, message.length, inetAddress, port); //发送数据包囊
MulticastSocket multicastSocket = new MulticastSocket();//创建组播socket
multicastSocket.send(datagramPacket);
}catch (Exception e) {
System.out.println("UdpTestServer send Exception: "+e.toString());
}
if(MessageIndex>=50){
closed = true;
}
}
/**
* @param args
*/
public static void main(String[] args) {
UdpGroupServer server = new UdpGroupServer();
server.start();
}
}
//运行效果
UdpTestServer start
received packet from 192.168.2.221 : 57242
message from client,0
received packet from 192.168.2.221 : 57245
hello ,message from server,0
received packet from 192.168.2.221 : 57248
hello ,message from server,1
组播地址:
组播组可以是永久的也可以是临时的。组播组地址中,有一部分由官方分配的,称为永久组播组。永久组播组保持不变的是它的ip地址,组中的成员构成可以发生变化。永久组播组中成员的数量都可以是任意的,甚至可以为零。那些没有保留下来供永久组播组使用的ip组播地址,可以被临时组播组利用。
224.0.0.0~224.0.0.255为预留的组播地址(永久组地址),地址224.0.0.0保留不做分配,其它地址供路由协议使用;
224.0.1.0~224.0.1.255是公用组播地址,可以用于Internet;
224.0.2.0~238.255.255.255为用户可用的组播地址(临时组地址),全网范围内有效;
239.0.0.0~239.255.255.255为本地管理组播地址,仅在特定的本地范围内有效。
问题:不同网段的设备广播是否收的到的?
情况一:单个交换机划分了多个vlan,不同设备在不同的vlan
结论: 显而易知,一个vlan就是一个广播域,那应该是收不到了
情况二: 单个或者多个交换机连接,划分一个vlan
结论: 收到的到,经我验证确实如此
情况三: 属于不同网络,但是上层通过路由器,使两个网络能访问。
结论: 收不到,路由器会隔离广播
一些网友问这个问题,刚好看了一些网络相关的知识,在此笔记一下,不对之处欢迎指导