Rust
Работа с сетевыми пакетами в Rust
Работа с сетевыми пакетами
93
#description
use std::net::{UdpSocket, Ipv4Addr, SocketAddrV4}; fn main() { let local_ip = "0.0.0.0"; let local_port = 12345; let socket_addr = SocketAddrV4::new(Ipv4Addr::from_str(local_ip).unwrap(), local_port); let socket = UdpSocket::bind(socket_addr).expect("Failed to bind socket"); let mut buf = [0; 1024]; loop { let (amt, src) = socket.recv_from(&mut buf).expect("Failed to receive data"); println!("Received {} bytes from {}", amt, src); socket.send_to(&buf[..amt], &src).expect("Failed to send data back"); } }
March 16, 2024