Stopping Content From Being Output Multiple Times via TCP?

I am writing a basic chat server and client to test how TCP connections work in Rust. The problem I am having right now is that each time I receive output from the server, it isn't just the most recent chat I've sent, but rather the entire output I've already received:
Please enter your name.

Please enter your name.
Hayden has connected to server.

Please enter your name.
Hayden has connected to server.
Hayden says hello

Please enter your name.
Hayden has connected to server.
Hayden says hello
Hayden says goodbye

Exiting.
Any thoughts on why this is happening?

Can you post your code? Without seeing what you're doing it could be anything. You may be forgetting to clear your buffer before you read the TCP stream into it each time, for instance.

1 Like

Mea culpa. I had forgotten that calls to BufReader::read_line don't replace the string they're passed but append to it. I was creating the string above the loop that reads from the clients, so the String just kept growing. Many thanks.

2 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.