# One Message Is Not a Conversation

Canonical: <https://journeytocode.io/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](https://github.com/journey-to-code-org/one-world-many-players/tree/article_1). 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:

**src/bin/server.rs**

```rust
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`:

**src/bin/client.rs**

```rust
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`.

**Question:** What does `read_line()` returning `0` mean for a TCP stream?

1. The player submitted a blank line
2. The server should try the same read again immediately
3. The other side closed its end of the connection

**Answer:** 3. The other side closed its end of the connection

> **Explanation:** A blank line still contains a newline byte. Zero bytes means the stream reached its end.


## What we learned

- `loop` repeats when the number of messages is unknown
- `break` ends the loop
- `continue` skips 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.**
[Join the discussion on the canonical article](https://journeytocode.io/one-message-is-not-a-conversation/#ob-discussion)
