Skip to content

Instantly share code, notes, and snippets.

@alexanderhenne
Created May 31, 2017 19:51
Show Gist options
  • Save alexanderhenne/74e5458ac3852ff8f259c1c3775ad010 to your computer and use it in GitHub Desktop.
Save alexanderhenne/74e5458ac3852ff8f259c1c3775ad010 to your computer and use it in GitHub Desktop.
use std::str::FromStr;
use std::net::TcpStream;
use std::io::BufReader;
use std::io::BufRead;
use std::io::BufWriter;
use std::io::Write;
const SERVER_ADDRESS: &'static str = "127.0.0.1:3214";
const REQUEST_ID: &'static str = "742786375";
const RESPONSE_ID: &'static str = "1065978963";
fn main() {
let stream = TcpStream::connect(SERVER_ADDRESS).unwrap();
let mut reader = BufReader::new(&stream);
let mut writer = BufWriter::new(&stream);
/*
Start hand-shaking process
*/
writer.write((String::from("hey ") + REQUEST_ID + "\r\n").as_bytes());
writer.flush();
loop {
let mut read_line = String::new();
reader.read_line(&mut read_line).unwrap();
let read_line = read_line.trim();
if read_line.len() != 0 {
if read_line != String::from("welcome ") + RESPONSE_ID {
println!("invalid response \"{}\"", read_line);
return;
}
break;
}
}
writer.write("😂\r\n".as_bytes());
writer.flush();
/*
Restart the read_line loop
after having shook hands
*/
loop {
let mut read_line = String::new();
reader.read_line(&mut read_line).unwrap();
let read_line = read_line.trim();
if read_line.len() != 0 {
println!("in: {}", read_line);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment