Skip to content

Instantly share code, notes, and snippets.

@Mazyod

Mazyod/bad_rust.rs

Created Oct 5, 2016
Embed
What would you like to do?
extern crate redis;
use redis::Commands;
use std::net::{TcpListener, TcpStream};
use std::thread;
use std::io::Read;
fn handle_client(mut stream: TcpStream) -> redis::RedisResult<()> {
let client = try!(redis::Client::open("redis://redis/"));
let con = try!(client.get_connection());
let mut buffer = [0; 2048];
let _ = stream.read(&mut buffer);
let s = format!("{}, Rust!", String::from_utf8_lossy(&buffer).trim());
println!("Received string: {} ..", s);
let _ = try!(con.set("rusty_key", s));
Ok(())
}
fn main() {
let listener = TcpListener::bind("0.0.0.0:3500").unwrap();
// accept connections and process them, spawning a new thread for each one
for stream in listener.incoming() {
match stream {
Ok(stream) => {
thread::spawn(move|| {
println!("Client connected!");
handle_client(stream);
});
}
Err(e) => {
println!("client errored!");
}
}
}
// close the socket server
drop(listener);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment