Created
August 28, 2017 03:16
-
-
Save matthunz/2242121a6a9c1f030302382dad6585b6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| use std::io::prelude::*; | |
| use std::net::{TcpListener, TcpStream}; | |
| use std::thread; | |
| fn main() { | |
| let listener = TcpListener::bind("127.0.0.1:6667").unwrap(); | |
| for stream in listener.incoming() { | |
| thread::spawn(move || { | |
| handle_client(stream.unwrap()); | |
| }); | |
| } | |
| } | |
| fn handle_client(mut stream: TcpStream) { | |
| loop { | |
| let mut buffer = [0; 512]; | |
| stream.read(&mut buffer).unwrap(); | |
| let line = String::from_utf8(buffer.to_vec()).unwrap(); | |
| let nick = match parse_nick(&line) { | |
| Ok(nick) => nick, | |
| Err(_) => break | |
| }; | |
| let msg = format!("001 {} :- Message of the Day", nick); | |
| if stream.write(msg.as_bytes()).is_err() { | |
| break; | |
| } | |
| } | |
| } | |
| fn parse_nick(line: &'static str) -> Result<&'static str, &'static str> { | |
| if line.contains("NICK") { | |
| Ok(line.split_at(5).1) | |
| } else { | |
| Err("Invalid nick command") | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment