//! Echo example. //! Use `nc -u 127.0.0.1 30000` to connect. use mini_rust_runtime::executor::Executor; use mini_rust_runtime::udp::UdpSocket; use tokio::io::{AsyncReadExt, AsyncWriteExt}; fn main() { let ex = Executor::new(); // 执行器实例 ex.block_on(server); // 协程最开始的地方 } async fn server() { let socket: UdpSocket = UdpSocket::bind("127.0.0.1:30000").unwrap(); let mut buf: [u8; 4096] = [0; 4096]; loop { match socket.recv_from_async(&mut buf).await { Ok((n, addr)) => { println!("recv_from {} {} bytes", addr, n); if n == 0 || socket.send_to_async(&buf[..n], &addr).await.is_err() { // 回写 return; } } Err(_) => { return; } } } }