Browse Source

A client that outputs what it receives

master
Julio Biason 3 years ago
parent
commit
9ba2af2daf
  1. 8
      ms/txtclient/Cargo.toml
  2. 21
      ms/txtclient/src/main.rs

8
ms/txtclient/Cargo.toml

@ -1,8 +1,16 @@
[package] [package]
name = "txtclient" name = "txtclient"
description = "A client that keeps listened to whatever the server is producing and shows on stdout"
version = "0.1.0" version = "0.1.0"
edition = "2021" edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [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" }

21
ms/txtclient/src/main.rs

@ -1,3 +1,20 @@
fn main() { use tokio::io::AsyncReadExt;
println!("Hello, world!"); 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);
}
} }

Loading…
Cancel
Save