From 98b01739600d6c71efbe3b40930163c7ea60d509 Mon Sep 17 00:00:00 2001 From: Ishan Jain Date: Sat, 9 Dec 2023 23:54:15 +0530 Subject: [PATCH] setup udp socket --- src/main.rs | 43 +++++++++++++++++++------------------------ 1 file changed, 19 insertions(+), 24 deletions(-) diff --git a/src/main.rs b/src/main.rs index eb5f125..f4d1223 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,28 +1,23 @@ -// Uncomment this block to pass the first stage -// use std::net::UdpSocket; +use std::net::UdpSocket; fn main() { - // You can use print statements as follows for debugging, they'll be visible when running tests. - println!("Logs from your program will appear here!"); + let udp_socket = UdpSocket::bind("127.0.0.1:2053").expect("Failed to bind to address"); + let mut buf = [0; 512]; - // Uncomment this block to pass the first stage - // let udp_socket = UdpSocket::bind("127.0.0.1:2053").expect("Failed to bind to address"); - // let mut buf = [0; 512]; - // - // loop { - // match udp_socket.recv_from(&mut buf) { - // Ok((size, source)) => { - // let _received_data = String::from_utf8_lossy(&buf[0..size]); - // println!("Received {} bytes from {}", size, source); - // let response = []; - // udp_socket - // .send_to(&response, source) - // .expect("Failed to send response"); - // } - // Err(e) => { - // eprintln!("Error receiving data: {}", e); - // break; - // } - // } - // } + loop { + match udp_socket.recv_from(&mut buf) { + Ok((size, source)) => { + let _received_data = String::from_utf8_lossy(&buf[0..size]); + println!("Received {} bytes from {}", size, source); + let response = []; + udp_socket + .send_to(&response, source) + .expect("Failed to send response"); + } + Err(e) => { + eprintln!("Error receiving data: {}", e); + break; + } + } + } }