1 use std::net::UdpSocket; 2 use std::str; 3 4 fn main() -> std::io::Result<()> { 5 let socket = UdpSocket::bind("127.0.0.1:34254")?; 6 socket.connect("127.0.0.1:34254")?; 7 8 let msg = "Hello, loopback!"; 9 socket.send(msg.as_bytes())?; 10 11 let mut buf = [0; 1024]; 12 let (amt, _src) = socket.recv_from(&mut buf)?; 13 14 let received_msg = str::from_utf8(&buf[..amt]).expect("Could not read buffer as UTF-8"); 15 16 println!("Sent: {}", msg); 17 println!("Received: {}", received_msg); 18 19 assert_eq!( 20 msg, received_msg, 21 "The sent and received messages do not match!" 22 ); 23 24 Ok(()) 25 } 26