Part 2 of 2
One Message Is Not a Conversation
Keep a Rust TCP Connection Alive With loop

Our client connects, receives one welcome line and disappears.
Technically, that is networking. It is not much of a game.
We are going to keep both sides alive until the player types quit. That gives us our first new Rust keywords: loop, break and continue. They appear because a connection has no fixed number of messages.
If you are building along, begin with the article_1 branch. It is the tested project at the end of the previous lesson.
Read until the connection ends#
Replace src/bin/server.rs with this version:
use std::io::{self, BufRead, BufReader, Write};
use std::net::{TcpListener, TcpStream};
fn handle_connection(mut stream: TcpStream) -> io::Result<()> {
let mut reader = BufReader::new(stream.try_clone()?);
writeln!(stream, "Welcome. Commands: look, quit")?;
loop {
let mut line = String::new();
let bytes = reader.read_line(&mut line)?;
if bytes == 0 {
break;
}
let command = line.trim();
if command.is_empty() {
continue;
}
if command == "quit" {
writeln!(stream, "Goodbye")?;
break;
}
writeln!(stream, "You entered: {command}")?;
}
Ok(())
}
fn main() -> io::Result<()> {
let listener = TcpListener::bind("127.0.0.1:4000")?;
println!("Server listening on 127.0.0.1:4000");
let (stream, address) = listener.accept()?;
println!("Connected: {address}");
handle_connection(stream)
}
loop has no condition. It keeps going until something inside chooses break.
There are two ways out here. The player sends quit, or read_line() returns 0. A zero-byte read does not mean the player sent an empty command. It means the stream reached the end because the other side closed the connection.
continue is different. A blank line should not end the connection, but it should not fall through and become a command either. continue skips the rest of this trip through the loop and starts the next one.
Let the client keep talking#
Replace src/bin/client.rs:
use std::io::{self, BufRead, BufReader, Write};
use std::net::TcpStream;
fn main() -> io::Result<()> {
let mut stream = TcpStream::connect("127.0.0.1:4000")?;
let mut reader = BufReader::new(stream.try_clone()?);
let stdin = io::stdin();
loop {
let mut reply = String::new();
if reader.read_line(&mut reply)? == 0 {
break;
}
print!("{reply}");
let mut command = String::new();
stdin.read_line(&mut command)?;
stream.write_all(command.as_bytes())?;
if command.trim() == "quit" {
let mut goodbye = String::new();
reader.read_line(&mut goodbye)?;
print!("{goodbye}");
break;
}
}
Ok(())
}
try_clone() does not open a second TCP connection. It creates another handle to the same operating-system socket, which lets the BufReader own one handle while we write through the other.
Run the server, then the client. Try a blank line, look, and quit.
What we learned#
looprepeats when the number of messages is unknownbreakends the loopcontinueskips the rest of one iteration- a zero-byte read means the connection closed
- two cloned handles can read and write the same socket
We can now carry as many strings as the player wants to send.
The server still has no idea what any of them mean.
One connection. Shared world.
Comments require JavaScript.