Skip to content

Commit

Permalink
feat: add client
Browse files Browse the repository at this point in the history
  • Loading branch information
care0717 committed Feb 28, 2021
1 parent f51b3f9 commit c7f79b5
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/bin/client.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use std::net::TcpStream;
use std::io::{Error, BufReader, BufWriter, Write, stdout, BufRead};

fn main() {
let stream = TcpStream::connect("127.0.0.1:33333");
match stream {
Ok(s) => {
handler(s).unwrap_or_else(|error| eprintln!("{:?}", error));
}
Err(e) => {eprintln!("error: {}", e)}
}
}

fn handler(stream: TcpStream) -> Result<(), Error> {
loop {
let mut input = String::new();
print!(">> ");
stdout().flush()?;

std::io::stdin().read_line(&mut input)?;

let mut writer = BufWriter::new(&stream);
writer.write(input.as_bytes())?;
writer.flush()?;
let mut reader = BufReader::new(&stream);
let mut return_value = String::new();
let nbytes = reader.read_line(&mut return_value)?;
if nbytes == 0 {
println!("receive EOF");
break
}
print!("{}", return_value);
}
return Ok(())
}

0 comments on commit c7f79b5

Please # to comment.