January 2018
Beginner to intermediate
454 pages
10h 8m
English
Let's start slowly by first creating a very simple server that sends "hello" to a new client and then closes the connection:
use std::net::TcpListener;
use std::io::Write;
fn main() {
let listener = TcpListener::bind("0.0.0.0:1234").expect("Couldn't bind this address...");
println!("Waiting for clients to connect...");
for stream in listener.incoming() {
Ok(stream) => {
println!("New client!);
if let Err(_) = stream.write(b"hello") {
println!("Failed to send hello... :'(");
}
}
_ => {
println!("A client tried to connect...")
}
}
}
Pretty easy, right? As usual, let's explain what the code does:
let listener = TcpListener::bind("0.0.0.0:1234").expect("Couldn't bind this address...");
For those who don't ...
Read now
Unlock full access