From 9ba2af2daf33145ec7cbb0b9995f7ae3a8bb38f9 Mon Sep 17 00:00:00 2001 From: Julio Biason Date: Thu, 18 Nov 2021 16:00:10 -0300 Subject: [PATCH] A client that outputs what it receives --- ms/txtclient/Cargo.toml | 8 ++++++++ ms/txtclient/src/main.rs | 21 +++++++++++++++++++-- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/ms/txtclient/Cargo.toml b/ms/txtclient/Cargo.toml index 95e552e..7095368 100644 --- a/ms/txtclient/Cargo.toml +++ b/ms/txtclient/Cargo.toml @@ -1,8 +1,16 @@ [package] name = "txtclient" +description = "A client that keeps listened to whatever the server is producing and shows on stdout" version = "0.1.0" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +bincode = "1.3.3" +env_logger = "0.9.0" +log = "0.4.14" +serde = { version = "1.0.130", features = ["derive"] } +tokio = { version = "1.14.0", features = ["rt", "net", "macros", "io-util"] } + +shared = { path = "../shared" } diff --git a/ms/txtclient/src/main.rs b/ms/txtclient/src/main.rs index e7a11a9..057c6a9 100644 --- a/ms/txtclient/src/main.rs +++ b/ms/txtclient/src/main.rs @@ -1,3 +1,20 @@ -fn main() { - println!("Hello, world!"); +use tokio::io::AsyncReadExt; +use tokio::net::TcpStream; + +use shared::Message; + +#[tokio::main(flavor = "current_thread")] +async fn main() { + env_logger::init(); + + let mut stream = TcpStream::connect("127.0.0.1:4435") + .await + .expect("Failed to connect to server"); + loop { + let mut buffer = [0; 1024]; + let bytes = stream.read(&mut buffer).await.expect("Failed to read data"); + let decoded: Message = + bincode::deserialize(&buffer[..bytes]).expect("Failed to convert message"); + log::info!("Got {:?}", decoded); + } }