From dd98493cac996e5f131a3e235c202820110ac975 Mon Sep 17 00:00:00 2001 From: Scott Worley Date: Tue, 10 Oct 2023 10:20:43 -0700 Subject: [PATCH 1/1] server: Receive packets --- server/src/main.rs | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) 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:?}"); + } + } + } } -- 2.44.1