概述
网络数据传播有单播、多播、广播几种形式。大部分的多播数据为音频或者视频,因为允许部分丢失,但是传输速度比TPCP快。
C类广播地址一般为XXX.XXX.XXX.255。例如,对于10.1.1.0 (255.255.255.0 )网段,其广播地址为10.1.1.255 (255 即为2 进制的11111111 ),当发出一个目的地址为10.1.1.255 的分组(封包)时,它将被分发给该网段上的所有计算机。
IP 地址的网络字段和主机字段全为 1 就是受限广播地址255.255.255.255。该地址用于主机配置过程中IP数据包的目的地址,此时,主机可能还不知道它所在网络的网络掩码,甚至连它的IP地址也不知道。在任何情况下,路由器都不转发目的地址为受限的广播地址的数据报,这样的数据报仅出现在本地网络中。
代码示例
可以利用受限广播地址完成局域网内好友的上线服务发现功能。
Provider:服务提供者。
Searcher:服务发现者。
每台服务设备Provider上线,在固定端口P监听,如果局域网内有服务搜索Searcher运行,会向255.255.255.255的P端口发送固定格式消息,消息里包含回送消息的端口。这样Provider收到消息之后,回送自己的ID给Searcher。
/**
* UDP 提供者,用于提供服务
*/
public class UDPProvider {
public static void main(String[] args) throws IOException {
// 生成一份唯一标示
String sn = UUID.randomUUID().toString();
Provider provider = new Provider(sn);
provider.start();
System.in.read();
provider.exit();
}
private static class Provider extends Thread {
private final String sn;
private boolean done = false;
private DatagramSocket ds = null;
public Provider(String sn) {
super();
this.sn = sn;
}
@Override
public void run() {
System.out.println("UDPProvider Started.");
try {
ds = new DatagramSocket(20000);
while (!done) {
final byte[] buf = new byte[512];
DatagramPacket receivePack = new DatagramPacket(buf, buf.length);
ds.receive(receivePack);
String ip = receivePack.getAddress().getHostAddress();
int port = receivePack.getPort();
int dataLen = receivePack.getLength();
String data = new String(receivePack.getData(), 0, dataLen);
System.out.println("UDPProvider receive form ip:" + ip + ",port:" + port + ",data:" + data);
// 解析端口号
int responsePort = Ciphertext.parsePort(data);
if (responsePort != -1) {
// 构建一份回送数据
String responseData = Ciphertext.buildWithSn(sn);
byte[] responseDataBytes = responseData.getBytes();
// 直接根据发送者构建一份回送信息
DatagramPacket responsePacket = new DatagramPacket(responseDataBytes, responseDataBytes.length,
receivePack.getAddress(),responsePort);
ds.send(responsePacket);
}
}
} catch (Exception ignored) {
} finally {
close();
}
System.out.println("UDPProvider Finished.");
}
private void close() {
if (ds != null) {
ds.close();
ds = null;
}
}
void exit() {
done = true;
close();
}
}
}
/**
* 通讯协议
*/
public class Ciphertext {
private static final String SN_HEADER = "ID";
private static final String PORT_HEADER = "PORT";
public static String buildWithPort(int port) {
return PORT_HEADER + port;
}
public static int parsePort(String data) {
if (data.startsWith(PORT_HEADER)) {
return Integer.parseInt(data.substring(PORT_HEADER.length()));
}
return -1;
}
public static String buildWithSn(String sn) {
return SN_HEADER + sn;
}
public static String parseSn(String data) {
if (data.startsWith(SN_HEADER)) {
return data.substring(SN_HEADER.length());
}
return null;
}
}
/**
* UDP 搜索者,用于搜索服务支持方
*/
public class UDPSearcher {
private static final int LISTEN_PORT = 30000;
public static void main(String[] args) throws IOException, InterruptedException {
System.out.println("UDPSearcher Started.");
Listener listener = listen();
sendBroadcast();
System.in.read();
List<Device> devices = listener.getDevicesAndClose();
for (Device device : devices) {
System.out.println("Device:" + device.toString());
}
// 完成
System.out.println("UDPSearcher Finished.");
}
private static Listener listen() throws InterruptedException {
System.out.println("UDPSearcher start listen.");
CountDownLatch countDownLatch = new CountDownLatch(1);
Listener listener = new Listener(LISTEN_PORT, countDownLatch);
listener.start();
countDownLatch.await();
return listener;
}
private static void sendBroadcast() throws IOException {
System.out.println("UDPSearcher sendBroadcast started.");
DatagramSocket ds = new DatagramSocket();
String requestData = Ciphertext.buildWithPort(LISTEN_PORT);
byte[] requestDataBytes = requestData.getBytes();
DatagramPacket requestPacket = new DatagramPacket(requestDataBytes, requestDataBytes.length);
requestPacket.setAddress(InetAddress.getByName("255.255.255.255"));
requestPacket.setPort(20000);
ds.send(requestPacket);
ds.close();
System.out.println("UDPSearcher sendBroadcast finished.");
}
private static class Device {
final int port;
final String ip;
final String sn;
private Device(int port, String ip, String sn) {
this.port = port;
this.ip = ip;
this.sn = sn;
}
@Override
public String toString() {
return "Device{" +
"port=" + port +
", ip='" + ip + '\'' +
", sn='" + sn + '\'' +
'}';
}
}
private static class Listener extends Thread {
private final int listenPort;
private final CountDownLatch countDownLatch;
private final List<Device> devices = new ArrayList<>();
private boolean done = false;
private DatagramSocket ds = null;
public Listener(int listenPort, CountDownLatch countDownLatch) {
super();
this.listenPort = listenPort;
this.countDownLatch = countDownLatch;
}
@Override
public void run() {
super.run();
// 通知已启动
countDownLatch.countDown();
try {
// 监听回送端口
ds = new DatagramSocket(listenPort);
while (!done) {
// 构建接收实体
final byte[] buf = new byte[512];
DatagramPacket receivePack = new DatagramPacket(buf, buf.length);
// 接收
ds.receive(receivePack);
// 打印接收到的信息与发送者的信息
// 发送者的IP地址
String ip = receivePack.getAddress().getHostAddress();
int port = receivePack.getPort();
int dataLen = receivePack.getLength();
String data = new String(receivePack.getData(), 0, dataLen);
System.out.println("UDPSearcher receive form ip:" + ip + ",port:" + port + ",data:" + data);
String sn = Ciphertext.parseSn(data);
if (sn != null) {
Device device = new Device(port, ip, sn);
devices.add(device);
}
}
} catch (Exception ignored) {
} finally {
close();
}
System.out.println("UDPSearcher listener finished.");
}
private void close() {
if (ds != null) {
ds.close();
ds = null;
}
}
List<Device> getDevicesAndClose() {
done = true;
close();
return devices;
}
}
}
运行