File: udp-echo.rs

package info (click to toggle)
rust-async-std 1.13.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,992 kB
  • sloc: sh: 13; makefile: 8
file content (26 lines) | stat: -rw-r--r-- 624 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
//! UDP echo server.
//!
//! To send messages, do:
//!
//! ```sh
//! $ nc -u localhost 8080
//! ```

use async_std::io;
use async_std::net::UdpSocket;
use async_std::task;

fn main() -> io::Result<()> {
    task::block_on(async {
        let socket = UdpSocket::bind("127.0.0.1:8080").await?;
        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);
        }
    })
}