use std::net::UdpSocket;
use std::io::{self, Read, Write};
fn main() -> io::Result<()> {
// 创建UDP套接字
let socket = UdpSocket::bind("127.0.0.1:8080")?;
// 初始化缓冲区
let mut buffer = [0; 1024];
loop {
// 等待数据包
let (number_of_bytes, sender_address) = socket.recv_from(&mut buffer)?;
// 解析数据包
let data = &buffer[..number_of_bytes];
// 处理数据(这里简单地将接收到的数据发送回客户端)
let response = b"Hello, UDP client!";
socket.send_to(&response, &sender_address)?;
// 打印接收到的数据
println!("Received message: {}", String::from_utf8_lossy(data));
}
}