Created
April 23, 2016 23:11
-
-
Save tilpner/3bacc74f7be476fe92541c05ea611ac3 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
[package] | |
authors = ["Till Höppner <till@hoeppner.ws>"] | |
name = "quux" | |
version = "0.1.0" | |
[dependencies] | |
mioco = "0.4.1" | |
time = "0.1.35" |
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
extern crate mioco; | |
extern crate time; | |
use std::str::FromStr; | |
use std::net::SocketAddr; | |
use std::io::prelude::*; | |
// use std::sync::{Arc, Mutex}; | |
use std::sync::Arc; | |
use mioco::sync::Mutex; | |
use std::io::BufReader; | |
use mioco::tcp::TcpStream; | |
fn get_time() -> f64 { | |
let tm = time::get_time(); | |
tm.sec as f64 + (tm.nsec as f64 / 1000000000.0) | |
} | |
fn main() { | |
mioco::start(|| { | |
let tcp_stream = TcpStream::connect(&SocketAddr::from_str("127.0.0.1:5555").unwrap()) | |
.unwrap(); | |
let mut client = BufReader::new(tcp_stream); | |
let mut client = Arc::new(Mutex::new(client)); | |
loop { | |
println!("iter!"); | |
let client = client.clone(); | |
let line = { | |
let mut client_lock = client.lock().unwrap(); | |
let mut line = String::new(); | |
client_lock.read_line(&mut line).unwrap(); | |
line | |
}; | |
if line.len() > 0 { | |
println!("line: {}", line); | |
} | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment