From: Scott Worley Date: Tue, 10 Oct 2023 17:20:43 +0000 (-0700) Subject: server: Receive packets X-Git-Tag: v0.1.0~20 X-Git-Url: http://git.scottworley.com/tattlekey/commitdiff_plain/dd98493cac996e5f131a3e235c202820110ac975?ds=inline;hp=891f78b762fa77f19899024d6d9977cf6e635467 server: Receive packets --- diff --git a/server/src/main.rs b/server/src/main.rs index e7a11a9..5a6315f 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -1,3 +1,21 @@ +use std::net::UdpSocket; + +const MESSAGE_SIZE: usize = 12; + fn main() { - println!("Hello, world!"); + let socket = UdpSocket::bind("0.0.0.0:29803").expect("couldn't bind to address"); + loop { + let mut buf = [0; MESSAGE_SIZE]; + match socket.recv_from(&mut buf) { + Err(e) => eprintln!("Didn't receive data: {e}"), + Ok((number_of_bytes, src_addr)) => { + let filled_buf = &mut buf[..number_of_bytes]; + if number_of_bytes != MESSAGE_SIZE { + eprintln!("Ignoring short message ({number_of_bytes}) from {src_addr}"); + continue; + } + println!("Got packet from {src_addr}: {filled_buf:?}"); + } + } + } }