当前位置:网站首页>【HBZ分享】UDP协议讲解
【HBZ分享】UDP协议讲解
2022-07-16 03:04:00 【hbz-】
UDP特点
- 面向无连接,即客户端不需要和服务端建立连接
- 安全性很低,不可靠协议,容易丢包
- 传输速度非常快,不需要像TCP那样三次握手确认服务端在
- 由于无视服务器端是否存在,客户端想发就发,就会导致,如果服务器端不存在,会出现数据丢失的情况
- 使用场景一般是【聊天工具】更多用的是UDP
UDP协议如何发送数据
- 创建发送端socket对象
- 发送者提供数据,并将数据封装到数据包
- 通过socket服务实现发送功能,将数据包发送出去
- 释放资源
UDP协议如何接收数据
- 创建接收的socket对象
- 接收数据
- 解析数据
- 输出数据
- 释放资源
UDP发送端的代码
public static void main(String[] args) throws IOException {
// 1. 创建发送端socket对象
DatagramSocket datagramSocket = new DatagramSocket();
Scanner sc = new Scanner(System.in);
while(true){
// 2. 发送者提供数据,并将数据封装到数据包
// 参数1:发送内容,byte数组类型
// 参数2:发送数据长度,即byte数组的长度
// 参数3:InetAddress对象,即【接收方】的ip信息
// 参数4:端口号port
String input = sc.nextLine();
byte[] msg = input.getBytes();
int msgLength = msg.length;
// 这个[hbz.test.udp.socket]可以是域名,可以是接受数据的IP地址,我已在本机host文件配置完毕,就是127.0.0.1 hbz.test.udp.socket
InetAddress inetAddress = InetAddress.getByName("hbz.test.udp.socket");
int port = 8080;
DatagramPacket datagramPacket = new DatagramPacket(msg, msgLength, inetAddress, port);
// 3. 通过socket服务实现发送功能,将数据包发送出去
datagramSocket.send(datagramPacket);
System.out.println("发送成功,数据内容 :" + new String(msg));
if("666".equals(new String(msg))){
break;
}
}
// 4. 释放资源
datagramSocket.close();
}
UDP接收端的代码
/**
* UDP接收端、
*
* 1. 创建接收的socket对象
* 2. 创建数据包对象,接收数据
* 3. 重点:解析数据
* 4. 输出数据
* 5. 释放资源
*/
public class UdpServer {
public static void main(String[] args) throws IOException {
String receiveStr = "";
// 1. 创建接收的socket对象, 指定使用8080端口接收,要和客户端对应上
DatagramSocket datagramSocket = new DatagramSocket(8080);
// 2. 接收数据 , 每次接受1024byte
byte[] receiveByte = new byte[1024];
// 参数1: 用什么来接收数据
// 参数2: 接收数据的长度
DatagramPacket datagramPacket = new DatagramPacket(receiveByte, receiveByte.length);
while (true) {
// 3. 重点:解析数据, 如果没有接收数据,这里会一直阻塞
datagramSocket.receive(datagramPacket);
// 4. 输出数据
receiveStr = new String(receiveByte);
System.out.println("接收数据内容:" + receiveStr);
if("666".equals(receiveStr)){
break;
}
}
System.out.println("客户端关闭了!");
// 5. 释放资源
datagramSocket.close();
}
}
边栏推荐
- Hcip dynamic routing experiment (RIP)
- 做测试/开发程序员的动力来自哪里?怎样坚持下去呢......
- 数字藏品系统开发,助力企业元宇宙场景营销
- hcip第一天学习--复习hcia(静态)
- Realsense d435 - camera internal parameter acquisition
- Constants and object freezing of typescript
- Runtimeerror: the size of tensor a (4) must match the size of tensor B (3) at non singleton
- Source insight 4 what about Chinese garbled code?
- Taking advantage of the trend, cool Lehman continued to set out with many pioneers
- Web3.0浪潮下,隐私计算与NFT的前景会是怎样
猜你喜欢

《通信软件开发与应用》结课报告

Where is the motivation to be a test / development programmer? How to persist

【Golang | gRPC】gRPC-Server Streaming服务端流实战

Harbor: modify the default 172 network segment

MySQL基础——增删查改(基础)

Network protocol -- concept and function analysis of seven layer, five layer and four layer protocols

股价跌穿!慕思这是要为“智商税”买单了吗?
![leetcode:1760. The least number of goal balls in the bag [the most valuable value + the scoreboard]](/img/f3/f7b038ef70ee0f1851dfda9bb793a9.png)
leetcode:1760. The least number of goal balls in the bag [the most valuable value + the scoreboard]

Kotlin compose bottom bar

MySQL基礎——新增與進階查詢
随机推荐
HCIP第三天学习笔记
Yin Yang translation
[QT入门篇]编程基础知识
《通信软件开发与应用》结课报告
[golang | GRC] GRC client streaming client streaming practice
hcip第五天筆記
Linux -- connecting to MySQL database and basic operations
2022年ICP许可证申办流程一览
Hcip day 4 notes
Programming old driver takes you to play with completable future asynchronous programming
Cache penetration, cache avalanche, cache breakdown?
JS small effect jump according to the corresponding item name
Analysis of Large Integer Decomposition
Where is the motivation to be a test / development programmer? How to persist
MySQL基礎——新增與進階查詢
网络协议——七层、五层、四层协议概念及功能分析
Viewing binlog and relay log information
Decentralized storage that lays the foundation of Web3
[today in history] July 15: Mozilla foundation was officially established; The first operation of Enigma cipher machine; Nintendo launches FC game console
ReversingKr-wp(7)