test program

This commit is contained in:
Lucas Schumacher 2023-09-23 22:25:10 -04:00
commit 7f234cabc2
4 changed files with 54 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/target

7
Cargo.lock generated Normal file
View File

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "tnc-tcp"
version = "0.1.0"

8
Cargo.toml Normal file
View File

@ -0,0 +1,8 @@
[package]
name = "tnc-tcp"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

38
src/main.rs Normal file
View File

@ -0,0 +1,38 @@
use std::io::{prelude::*, stdin};
use std::net::TcpStream;
use std::thread;
fn raw_to_string(rawdata: &[u8]) -> String {
let mut data = String::new();
for byte in rawdata.iter() {
if *byte >= 32 && *byte >= 126 {
data.push((*byte).into());
} else {
data.push_str(&format!("\\{:x?} ", *byte));
}
}
data
}
fn main() {
println!("Hello, world!");
let mut control = TcpStream::connect("localhost:8515").unwrap();
let mut ctl = control.try_clone().unwrap();
let mut data = TcpStream::connect("localhost:8516").unwrap();
thread::spawn(move || {
let mut buf: Vec<u8> = vec![0; 4096];
while let Ok(len) = data.read(&mut buf) {
println!("DATA: {}", raw_to_string(&buf[..len]));
}
});
thread::spawn(move || {
let mut buf: Vec<u8> = vec![0; 2046];
while let Ok(len) = ctl.read(&mut buf) {
println!("CTLR: {}", raw_to_string(&buf[..len]));
}
});
let mut input = String::new();
while let Ok(_n) = stdin().read_line(&mut input) {
input.push('\r');
control.write(input.as_bytes()).unwrap();
}
}