Skip to content

Instantly share code, notes, and snippets.

@KibaFox
Created November 20, 2016 20:20
Show Gist options
  • Save KibaFox/4cb421f4882ba2805c6331562f462b60 to your computer and use it in GitHub Desktop.
Save KibaFox/4cb421f4882ba2805c6331562f462b60 to your computer and use it in GitHub Desktop.
Simple Rust TCP Server
// Simple TCP server
// This is a simple tcp server written in rust that will print any line sent to
// it. It listens on port 5000 and you can use telnet to send lines of text.
use std::net::{TcpListener, TcpStream};
use std::io::{BufRead, BufReader};
fn main() {
let listener = TcpListener::bind("127.0.0.1:5000").unwrap();
fn handle_client(stream: TcpStream) {
let mut reader = BufReader::new(stream.copy());
loop {
let mut line = String::new();
let _ = reader.read_line(&mut line);
println!("{}", line);
}
}
for stream in listener.incoming() {
match stream {
Ok(stream) => {
handle_client(stream);
}
Err(_) => {
// connection failed
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment