feat: add rust tcp

This commit is contained in:
2021-02-17 12:10:11 +08:00
parent ed41681faf
commit 38439e4204
7 changed files with 1753 additions and 0 deletions

View File

@@ -0,0 +1,27 @@
use std::io::prelude::*;
use std::{io, thread};
fn main() -> io::Result<()> {
let mut i = trust::Interface::new()?;
eprintln!("created interface");
let mut listener = i.bind(8000)?;
while let Ok(mut stream) = listener.accept() {
eprintln!("got connection!");
thread::spawn(move || {
stream.write(b"hello from rust-tcp!\n").unwrap();
stream.shutdown(std::net::Shutdown::Write).unwrap();
loop {
let mut buf = [0; 512];
let n = stream.read(&mut buf[..]).unwrap();
eprintln!("read {}b of data", n);
if n == 0 {
eprintln!("no more data!");
break;
} else {
println!("{}", std::str::from_utf8(&buf[..n]).unwrap());
}
}
});
}
Ok(())
}