You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
async-std/examples/udp-echo.rs

27 lines
624 B
Rust

5 years ago
//! UDP echo server.
//!
//! To send messages, do:
//!
//! ```sh
//! $ nc -u localhost 8080
//! ```
5 years ago
use async_std::io;
use async_std::net::UdpSocket;
use async_std::task;
5 years ago
fn main() -> io::Result<()> {
task::block_on(async {
5 years ago
let socket = UdpSocket::bind("127.0.0.1:8080").await?;
5 years ago
let mut buf = vec![0u8; 1024];
println!("Listening on {}", socket.local_addr()?);
loop {
let (n, peer) = socket.recv_from(&mut buf).await?;
let sent = socket.send_to(&buf[..n], &peer).await?;
println!("Sent {} out of {} bytes to {}", sent, n, peer);
}
})
}