aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
blob: 464716f9db5502f60292b36291e752105eb90216 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#![allow(unused_imports)]
use std::{
    io::{Read, Write},
    net::TcpListener,
};

fn main() {
    let listener = TcpListener::bind("127.0.0.1:6379").unwrap();

    for stream in listener.incoming() {
        match stream {
            Ok(mut stream) => {
                let mut buf = [0; 128];
                stream.read(&mut buf).unwrap();
                let request = str::from_utf8(&buf).unwrap();
                for _ in 0..request.matches("PING").count() {
                    stream.write_all(b"+PONG\r\n").unwrap();
                }
            }
            Err(e) => {
                println!("error: {}", e);
            }
        }
    }
}