Skip to content

Instantly share code, notes, and snippets.

@Inari-Whitebear
Created January 28, 2015 19:48
Show Gist options
  • Save Inari-Whitebear/35092eb2e21820929ca2 to your computer and use it in GitHub Desktop.
Save Inari-Whitebear/35092eb2e21820929ca2 to your computer and use it in GitHub Desktop.
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