Skip to content

Commit 3710526

Browse files
committed
Add simple tool for TCP loopback testing
Monitor your board's serial log for the IP address it's assigned. Then, pass it into this command-line tool to make sure you can connect, send a message, and receive the response.
1 parent 7428b13 commit 3710526

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ eh02-unproven = []
8787
members = [
8888
"board",
8989
"logging",
90+
"tools",
9091
]
9192

9293
[workspace.dependencies]

tools/Cargo.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[package]
2+
name = "tools"
3+
version = "0.1.0"
4+
repository.workspace = true
5+
keywords.workspace = true
6+
categories.workspace = true
7+
license.workspace = true
8+
edition.workspace = true
9+
publish = false
10+
11+
[dependencies]

tools/src/bin/tcp_loopback.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
use std::io::prelude::*;
2+
use std::net::TcpStream;
3+
4+
fn main() -> Result<(), Box<dyn std::error::Error>> {
5+
let ip = std::env::args()
6+
.skip(1)
7+
.next()
8+
.ok_or("Provide an IPv4 address")?;
9+
10+
let ip: std::net::Ipv4Addr = ip.parse()?;
11+
12+
let mut client = TcpStream::connect(std::net::SocketAddrV4::new(ip, 5000))?;
13+
14+
const MSG: &[u8] = b"Hello, world!";
15+
client.write(MSG)?;
16+
let mut resp = [0; MSG.len()];
17+
client.read(&mut resp)?;
18+
19+
if resp == MSG {
20+
Ok(())
21+
} else {
22+
Err(format!("Expected '{MSG:?}' but received '{resp:?}'").into())
23+
}
24+
}

0 commit comments

Comments
 (0)