more code commants and reorganization for better redability

This commit is contained in:
Norbert Morawski 2023-07-08 01:58:31 +02:00
parent 0d06438230
commit 4110778123

View File

@ -1,13 +1,14 @@
use tokio::net::{TcpListener, TcpStream};
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use std::collections::HashMap;
use std::env;
use std::error::Error;
use std::collections::HashMap;
use std::net::SocketAddr;
use std::sync::{Arc, Mutex};
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::{TcpListener, TcpStream};
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
// Parse command-line arguments
let args: Vec<String> = env::args().collect();
if args.len() != 3 {
println!("Usage: {} [server|client] [addr:port]", args[0]);
@ -27,52 +28,59 @@ async fn main() -> Result<(), Box<dyn Error>> {
}
async fn start_server(addr: &str) -> Result<(), Box<dyn Error>> {
// Start the TCP listener
let listener = TcpListener::bind(addr).await?;
println!("Server listening on {}", addr);
// Shared data for client connections
let clients: Arc<Mutex<HashMap<SocketAddr, String>>> = Arc::new(Mutex::new(HashMap::new()));
loop {
// Accept a new client connection
let (mut socket, client_addr) = listener.accept().await?;
// Read username from the client
let mut buf = vec![0; 1024];
socket.read(&mut buf).await?;
let username = String::from_utf8(buf.clone()).unwrap();
println!("Accepted connection from {}", client_addr);
let clients_shared = Arc::clone(&clients);
// Insert client into the shared HashMap
clients_shared.lock().unwrap().insert(client_addr, username);
// Spawn a new task for each client connection
tokio::spawn(async move {
let mut buf = vec![0; 1024];
loop {
let n = match socket.read(&mut buf).await {
Ok(n) if n == 0 => {
// Client disconnected
let username = clients_shared.lock().unwrap().remove(&client_addr).unwrap_or("Unknown".to_string());
println!("Client {} ({}) disconnected!", username, client_addr);
return;
},
}
Ok(n) => n,
Err(e) => {
eprintln!("failed to read from socket; err = {:?}", e);
eprintln!("Failed to read from socket; err = {:?}", e);
return;
}
};
let username = clients_shared.lock().unwrap().get(&client_addr).unwrap_or(&"Unknown".to_string()).clone();
println!("Received message from {} ({}): {}",
println!(
"Received message from {} ({}): {}",
username,
client_addr,
String::from_utf8_lossy(&buf[..n]));
String::from_utf8_lossy(&buf[..n])
);
if let Err(e) = socket.write_all(&buf[0..n]).await {
eprintln!("failed to write to socket; err = {:?}", e);
eprintln!("Failed to write to socket; err = {:?}", e);
return;
}
}
@ -81,12 +89,14 @@ async fn start_server(addr: &str) -> Result<(), Box<dyn Error>> {
}
async fn start_client(addr: &str) -> Result<(), Box<dyn Error>> {
// Connect to the server
let mut stream = TcpStream::connect(addr).await?;
println!("Enter a username: ");
let mut username = String::new();
let mut username = String::new();
std::io::stdin().read_line(&mut username)?;
// Send username to the server
stream.write_all(username.as_bytes()).await?;
println!("Connected to server at {}", addr);