more rewrites to tokio

This commit is contained in:
Norbert Morawski 2023-07-08 05:47:58 +02:00
parent 57b35902ae
commit 8ff5112352

View File

@ -1,9 +1,9 @@
use std::collections::HashMap;
use std::env;
use std::error::Error;
use std::net::SocketAddr;
use std::sync::Arc;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use std::collections::HashMap;
use tokio::io::{AsyncReadExt, AsyncBufReadExt, AsyncWriteExt, BufReader};
use tokio::net::{TcpListener, TcpStream};
use tokio::sync::Mutex;
@ -94,7 +94,8 @@ async fn start_client(addr: &str) -> Result<(), Box<dyn Error>> {
println!("Enter a username: ");
let mut username = String::new();
std::io::stdin().read_line(&mut username)?;
let mut reader = BufReader::new(tokio::io::stdin());
reader.read_line(&mut username).await?;
// Send username to the server
stream.write_all(username.trim().as_bytes()).await?;
@ -102,7 +103,7 @@ async fn start_client(addr: &str) -> Result<(), Box<dyn Error>> {
println!("Connected to server at {}", addr);
let mut input = String::new();
loop {
std::io::stdin().read_line(&mut input)?;
reader.read_line(&mut input).await?;
stream.write_all(input.trim().as_bytes()).await?;
// Print sent message
@ -110,3 +111,4 @@ async fn start_client(addr: &str) -> Result<(), Box<dyn Error>> {
input.clear();
}
}