setup udp socket

This commit is contained in:
Ishan Jain 2023-12-09 23:54:15 +05:30
parent 115c51577f
commit 98b0173960
Signed by: ishan
GPG Key ID: 0506DB2A1CC75C27

View File

@ -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;
}
}
}
}