1 // You can run this example from the root of the mio repo:
2 // cargo run --example udp_server --features="os-poll udp"
3 use log::warn;
4 use mio::net::UdpSocket;
5 use mio::{Events, Interest, Poll, Token};
6 use std::io;
7
8 // A token to allow us to identify which event is for the `UdpSocket`.
9 const UDP_SOCKET: Token = Token(0);
10
main() -> io::Result<()>11 fn main() -> io::Result<()> {
12 env_logger::init();
13
14 // Create a poll instance.
15 let mut poll = Poll::new()?;
16 // Create storage for events. Since we will only register a single socket, a
17 // capacity of 1 will do.
18 let mut events = Events::with_capacity(1);
19
20 // Setup the UDP socket.
21 let addr = "127.0.0.1:9000".parse().unwrap();
22 let mut socket = UdpSocket::bind(addr)?;
23
24 // Register our socket with the token defined above and an interest in being
25 // `READABLE`.
26 poll.registry()
27 .register(&mut socket, UDP_SOCKET, Interest::READABLE)?;
28
29 println!("You can connect to the server using `nc`:");
30 println!(" $ nc -u 127.0.0.1 9000");
31 println!("Anything you type will be echoed back to you.");
32
33 // Initialize a buffer for the UDP packet. We use the maximum size of a UDP
34 // packet, which is the maximum value of 16 a bit integer.
35 let mut buf = [0; 1 << 16];
36
37 // Our event loop.
38 loop {
39 // Poll to check if we have events waiting for us.
40 poll.poll(&mut events, None)?;
41
42 // Process each event.
43 for event in events.iter() {
44 // Validate the token we registered our socket with,
45 // in this example it will only ever be one but we
46 // make sure it's valid none the less.
47 match event.token() {
48 UDP_SOCKET => loop {
49 // In this loop we receive all packets queued for the socket.
50 match socket.recv_from(&mut buf) {
51 Ok((packet_size, source_address)) => {
52 // Echo the data.
53 socket.send_to(&buf[..packet_size], source_address)?;
54 }
55 Err(e) if e.kind() == io::ErrorKind::WouldBlock => {
56 // If we get a `WouldBlock` error we know our socket
57 // has no more packets queued, so we can return to
58 // polling and wait for some more.
59 break;
60 }
61 Err(e) => {
62 // If it was any other kind of error, something went
63 // wrong and we terminate with an error.
64 return Err(e);
65 }
66 }
67 },
68 _ => {
69 // This should never happen as we only registered our
70 // `UdpSocket` using the `UDP_SOCKET` token, but if it ever
71 // does we'll log it.
72 warn!("Got event for unexpected token: {:?}", event);
73 }
74 }
75 }
76 }
77 }
78