expression

Connected UDP sockets

Set the default peer for a UDP socket and restrict incoming datagrams to that peer. This is not a TCP-style handshake or proof of peer reachability. Tokio is an external crate. Authorized isolated IPv4 loopback fixture; no public network or DNS. Expected stdout (not executed): ok Common mistake: Assuming UDP connect proves remote availability.

Formal Syntax & Structure

rust syntaxrust
socket.connect(peer).await; send; recv

Working Example

Example Snippetrust
#[tokio::main(flavor = "current_thread")]async fn main() {    tokio::time::timeout(std::time::Duration::from_secs(3), async {        use tokio::net::UdpSocket;        let sender = UdpSocket::bind("127.0.0.1:0").await.unwrap();        let receiver = UdpSocket::bind("127.0.0.1:0").await.unwrap();        sender.connect(receiver.local_addr().unwrap()).await.unwrap();        receiver.connect(sender.local_addr().unwrap()).await.unwrap();        sender.send(b"ok").await.unwrap();        let mut b = [0; 8];        let n = receiver.recv(&mut b).await.unwrap();        println!("{}", std::str::from_utf8(&b[..n]).unwrap());    }).await.expect("fixture deadline");}
Rust Reference