1筋栋、UDP
- 用戶(hù)數(shù)據(jù)報(bào)協(xié)議炊汤,又稱(chēng)用戶(hù)數(shù)據(jù)報(bào)文協(xié)議
- 簡(jiǎn)單的面向數(shù)據(jù)報(bào)的傳輸層協(xié)議
- 用戶(hù)數(shù)據(jù)協(xié)議,非連接協(xié)議
- 不可靠弊攘、結(jié)構(gòu)簡(jiǎn)單抢腐、無(wú)校驗(yàn)、速度快襟交、容易丟包迈倍、可廣播
2.應(yīng)用
- DNS、TFTP捣域、SNMP
- 視頻啼染、音頻(直播);普通數(shù)據(jù)(無(wú)關(guān)緊要數(shù)據(jù))
3.UDP包最大長(zhǎng)度
image.png
image.png
4.UDP局域網(wǎng)搜索
4.1UDP 提供者焕梅,用于提供服務(wù)
public class UDPProvider {
public static void main(String[] args) throws IOException {
// 生成一份唯一標(biāo)示
String sn = UUID.randomUUID().toString();
Provider provider = new Provider(sn);
provider.start();
// 讀取任意鍵盤(pán)信息后可以退出
//noinspection ResultOfMethodCallIgnored
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() {
super.run();
System.out.println("UDPProvider Started.");
try {
// 監(jiān)聽(tīng)20000 端口
ds = new DatagramSocket(20000);
while (!done) {
// 構(gòu)建接收實(shí)體
final byte[] buf = new byte[512];
DatagramPacket receivePack = new DatagramPacket(buf, buf.length);
// 接收
ds.receive(receivePack);
// 打印接收到的信息與發(fā)送者的信息
// 發(fā)送者的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("UDPProvider receive form ip:" + ip
+ "\tport:" + port + "\tdata:" + data);
// 解析端口號(hào)
int responsePort = MessageCreator.parsePort(data);
if (responsePort != -1) {
// 構(gòu)建一份回送數(shù)據(jù)
String responseData = MessageCreator.buildWithSn(sn);
byte[] responseDataBytes = responseData.getBytes();
// 直接根據(jù)發(fā)送者構(gòu)建一份回送信息
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;
}
}
/**
* 提供結(jié)束
*/
void exit() {
done = true;
close();
}
}
}
4.2UDP 搜索者迹鹅,用于搜索服務(wù)支持方
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();
// 讀取任意鍵盤(pán)信息后可以退出
//noinspection ResultOfMethodCallIgnored
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.");
// 作為搜索方,讓系統(tǒng)自動(dòng)分配端口
DatagramSocket ds = new DatagramSocket();
// 構(gòu)建一份請(qǐng)求數(shù)據(jù)
String requestData = MessageCreator.buildWithPort(LISTEN_PORT);
byte[] requestDataBytes = requestData.getBytes();
// 直接構(gòu)建packet
DatagramPacket requestPacket = new DatagramPacket(requestDataBytes,
requestDataBytes.length);
// 20000端口, 廣播地址
requestPacket.setAddress(InetAddress.getByName("255.255.255.255"));
requestPacket.setPort(20000);
// 發(fā)送
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;
/**
* 同步輔助類(lèi)
*/
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();
// 通知已啟動(dòng)
countDownLatch.countDown();
try {
// 監(jiān)聽(tīng)回送端口
ds = new DatagramSocket(listenPort);
while (!done) {
// 構(gòu)建接收實(shí)體
final byte[] buf = new byte[512];
DatagramPacket receivePack = new DatagramPacket(buf, buf.length);
// 接收
ds.receive(receivePack);
// 打印接收到的信息與發(fā)送者的信息
// 發(fā)送者的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
+ "\tport:" + port + "\tdata:" + data);
String sn = MessageCreator.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;
}
}
}
public class MessageCreator {
private static final String SN_HEADER = "收到暗號(hào)贞言,我是(SN):";
private static final String PORT_HEADER = "這是暗號(hào)斜棚,請(qǐng)回電端口(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;
}
}