forked from mirror/async-std
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.
35 lines
765 B
Rust
35 lines
765 B
Rust
//! UDP client.
|
|
//!
|
|
//! First start the echo server:
|
|
//!
|
|
//! ```sh
|
|
//! $ cargo run --example udp-echo
|
|
//! ```
|
|
//!
|
|
//! Then run the client:
|
|
//!
|
|
//! ```sh
|
|
//! $ cargo run --example udp-client
|
|
//! ```
|
|
|
|
#![feature(async_await)]
|
|
|
|
use async_std::{io, net::UdpSocket, task};
|
|
|
|
fn main() -> io::Result<()> {
|
|
task::block_on(async {
|
|
let socket = UdpSocket::bind("127.0.0.1:8081").await?;
|
|
println!("Listening on {}", socket.local_addr()?);
|
|
|
|
let msg = "hello world";
|
|
println!("<- {}", msg);
|
|
socket.send_to(msg.as_bytes(), "127.0.0.1:8080").await?;
|
|
|
|
let mut buf = vec![0u8; 1024];
|
|
let (n, _) = socket.recv_from(&mut buf).await?;
|
|
println!("-> {}\n", String::from_utf8_lossy(&buf[..n]));
|
|
|
|
Ok(())
|
|
})
|
|
}
|