Make udp-test bi-directional

This commit is contained in:
Travis Burtrum 2019-12-17 01:28:43 -05:00
parent 1e7fa84a42
commit da86f8da72
1 changed files with 18 additions and 2 deletions

View File

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