2
0
Fork 1
mirror of https://github.com/async-rs/async-std.git synced 2025-01-16 10:49:55 +00:00
async-std/examples/udp-echo.rs

27 lines
624 B
Rust
Raw Normal View History

2019-08-08 12:44:48 +00:00
//! UDP echo server.
//!
//! To send messages, do:
//!
//! ```sh
//! $ nc -u localhost 8080
//! ```
2019-08-14 01:47:39 +00:00
use async_std::io;
use async_std::net::UdpSocket;
use async_std::task;
2019-08-08 12:44:48 +00:00
fn main() -> io::Result<()> {
task::block_on(async {
2019-08-12 10:44:57 +00:00
let socket = UdpSocket::bind("127.0.0.1:8080").await?;
2019-08-08 12:44:48 +00:00
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);
}
})
}