Created
January 28, 2015 19:48
-
-
Save Inari-Whitebear/35092eb2e21820929ca2 to your computer and use it in GitHub Desktop.
This file contains 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::{TcpListener, TcpStream}; | |
use std::io::{Acceptor, Listener}; | |
use std::str; | |
fn handle_client(mut stream: TcpStream) { | |
let mut buf: [u8; 1024] = [0; 1024]; | |
let read_size = stream.read(&mut buf); | |
match read_size { | |
Ok(s) => { | |
let mut ss = String::from_str(str::from_utf8(&buf)); | |
println!("Connected, read data size: {0}, data: {1}", s, ss); | |
}, | |
Err(e) => { | |
println!("error reading stream: {}", e); | |
} | |
}; | |
} | |
fn main() { | |
let listener = TcpListener::bind("127.0.0.1:5555").unwrap(); | |
let mut acceptor = listener.listen().unwrap(); | |
for stream in acceptor.incoming() { | |
match stream { | |
Err(e) => { println!("Connection failed!"); }, | |
Ok(stream) => { | |
handle_client(stream); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment