In this exercise, we will implement a simple TCP server.

You will learn:

  • How to write a simple single-threaded server binary

  • Read Strings from the network

  • How to handle a connection lifecycle


  1. Accept TCP connections on port

  2. Read all incoming data as String

  3. Print the data to the console

  4. Echo it back to the client

Getting started

Use this template:

use std::net::{TcpListener, TcpStream};
use std::io;

fn handle_client(mut stream: TcpStream) -> Result<(), io::Error> {
    // ...

fn main() -> io::Result<()> {
    let listener = TcpListener::bind("")?;

    // accept connections and process them serially
    for stream in listener.incoming() {

Read the documentation for the std::io::Read, std::io::Write traits, especially Read::read_to_string and Write::write_all.


Blanket import of io functionality

use std::io::prelude::*;

Writing Strings as bytes


Sending Data

To send test data to your server, you can use our example client implementation:

$ git clone
$ cd teaching-material/assignments/solutions/tcp-client
$ cargo run testmessage