From da86f8da72baa7fbb1b0718ccbfaec031359a152 Mon Sep 17 00:00:00 2001 From: moparisthebest Date: Tue, 17 Dec 2019 01:28:43 -0500 Subject: [PATCH] Make udp-test bi-directional --- src/bin/udp-test.rs | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/bin/udp-test.rs b/src/bin/udp-test.rs index f48dd7d..b5be39e 100644 --- a/src/bin/udp-test.rs +++ b/src/bin/udp-test.rs @@ -50,6 +50,22 @@ impl Server { assert_eq!(sent, PONG.len()); let mut buf = [0u8; 2048]; + match udp_socket.recv_from(&mut buf) { + Ok((len, src_addr)) => { + println!("udp got len: {} from src_addr: {}", len, src_addr); + assert_eq!(len, PONG.len()); + assert_eq!(&buf[..len], &PONG[..]); + + // now reply back to src_addr to make sure other direction works + let sent = udp_socket.send_to(&PONG, &src_addr)?; + assert_eq!(sent, PONG.len()); + } + Err(e) => { + panic!("recv function failed first round: {:?}", e); + } + } + + // if we are here, we successfully sent the reply, did we get it back? match udp_socket.recv(&mut buf) { Ok(len) => { println!("udp got len: {}", len); @@ -57,11 +73,11 @@ impl Server { assert_eq!(&buf[..len], &PONG[..]); } Err(e) => { - panic!("recv function failed: {:?}", e); + panic!("recv function failed second round: {:?}", e); } } - println!("success! received back exactly what we sent!"); + println!("success! received back exactly what we sent both ways!"); Ok(0) }