Properly detect client disconnect
This commit is contained in:
parent
28b85b1d6c
commit
3e8e9444a5
12
src/main.rs
12
src/main.rs
|
@ -28,15 +28,13 @@ async fn handle_connection(
|
|||
println!("IP Address: {}", a);
|
||||
clients.lock().await.insert(a, 0);
|
||||
loop {
|
||||
let (sockread, mut sockwrite) = socket.split();
|
||||
|
||||
if sockwrite.write_all(b"PING\n").await.is_err() {
|
||||
break;
|
||||
};
|
||||
|
||||
let mut buf = [0u8; 1024];
|
||||
|
||||
let bytes = sockread.take(1024u64).read(&mut buf).await.unwrap();
|
||||
let bytes = match socket.read(&mut buf).await {
|
||||
Ok(0) => break, // eof
|
||||
Ok(n) => n,
|
||||
Err(e) => panic!("error occurred: {}", e)
|
||||
};
|
||||
print!("{}", String::from_utf8_lossy(&buf[..bytes]).to_string());
|
||||
}
|
||||
println!("Client disconnected");
|
||||
|
|
Loading…
Reference in a new issue