]> git.scottworley.com Git - tattlekey/commitdiff
server: Receive packets
authorScott Worley <scottworley@scottworley.com>
Tue, 10 Oct 2023 17:20:43 +0000 (10:20 -0700)
committerScott Worley <scottworley@scottworley.com>
Wed, 11 Oct 2023 01:50:20 +0000 (18:50 -0700)
server/src/main.rs

index e7a11a969c037e00a796aafeff6258501ec15e9a..5a6315f724e0557ede8447a9a98b38f104d949d8 100644 (file)
@@ -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:?}");
+            }
+        }
+    }
 }