Skip to content

Instantly share code, notes, and snippets.

@klingtnet
Created October 29, 2018 19:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save klingtnet/134d88c6bb151003712a542a06961dfc to your computer and use it in GitHub Desktop.
Save klingtnet/134d88c6bb151003712a542a06961dfc to your computer and use it in GitHub Desktop.
Rust TCP to STDOUT server
use std::io;
use std::net::{TcpListener, TcpStream};
fn handle_client(mut stream: TcpStream) {
let sout = io::stdout();
let mut sout_handle = sout.lock();
io::copy(&mut stream, &mut sout_handle).expect("failed to copy into stdout");
}
fn main() -> io::Result<()> {
let listener = TcpListener::bind("127.0.0.1:8000")?;
for stream in listener.incoming() {
// It's a single connection server :)
// Ok, we could spawn a thread here but
// stdout is blocked anyways (see handle_client).
handle_client(stream.expect("failed to unwrap stream"));
}
Ok(())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment