Skip to content

Instantly share code, notes, and snippets.

@Mazyod
Created October 5, 2016 16:52
Show Gist options
  • Save Mazyod/8f6807e2c859b3457eaa4bcf24d5d703 to your computer and use it in GitHub Desktop.
Save Mazyod/8f6807e2c859b3457eaa4bcf24d5d703 to your computer and use it in GitHub Desktop.
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