Skip to content

Instantly share code, notes, and snippets.

@tilpner
Created April 23, 2016 23:11
Show Gist options
  • Save tilpner/3bacc74f7be476fe92541c05ea611ac3 to your computer and use it in GitHub Desktop.
Save tilpner/3bacc74f7be476fe92541c05ea611ac3 to your computer and use it in GitHub Desktop.
[package]
authors = ["Till Höppner <till@hoeppner.ws>"]
name = "quux"
version = "0.1.0"
[dependencies]
mioco = "0.4.1"
time = "0.1.35"
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