Skip to content

Echo Server

You'll open a TCP socket, talk to it with netcat, and go from serving one client at a time to juggling several with poll.

About

Sockets are files that happen to go somewhere else, and the five calls that make a server (socket, bind, listen, accept, read) fit on an index card. You'll write a server that sends back whatever it gets, test it with netcat from a second terminal, and then hit the wall where the second client has to wait for the first. Fixing that with poll is your first taste of handling many connections, which the Rare badges build on. Bind to 127.0.0.1 only, because a server you've just written has no business on the open network yet.

At a glance

Per requirement
25 XP
Completion bonus
150 XP
Requirements
6
Takes
One weekend
Skill level
Beginner (done it a few times)
Cost
Free
Supplies
You need some
Where
At home
Other people
On your own
Locked for now
Earn two more Common badges in Systems Programming to open this one. Log in to see how far along you are.

Requirements

  1. 1
    Write a TCP echo server on localhost and talk to it with netcat. Use socket, bind, listen and accept, then read into a buffer and write it back in a loop until the client disconnects. Bind to 127.0.0.1 on a port above 1024, and set SO_REUSEADDR before bind or you'll wait a minute to restart after every crash. Open nc 127.0.0.1 PORT in a second terminal and type at it. Film a short clip of typing a line and seeing it come back. Video
  2. 2
    Show the one-client problem by connecting two netcats at once. With your server accepting one connection and serving it to the end, open two netcat sessions. The second will connect but get nothing back until the first closes. Take a screenshot of both terminals with the second one stuck. Photo
  3. 3
    Rewrite the accept loop with poll so several clients get served at once. Keep an array of file descriptors, poll them all, and handle whichever is ready. When read returns 0, the client has gone, so close the descriptor and drop it from the array. Connect three netcats and show all three echo. Film a short clip of three clients working at the same time. Video
  4. 4
    Handle a client that disconnects mid-write without crashing the server. Kill a netcat while the server is sending to it. By default the OS raises SIGPIPE and your server dies, so ignore that signal or use MSG_NOSIGNAL, and check the return value of every write. Take a screenshot of the server still running after the client is killed. Photo
  5. 5
    Write a matching client in C or Rust that sends a line, prints the reply and exits. Use socket and connect, then write, read and close. Add a timeout with setsockopt or poll so it doesn't hang forever if the server is down. Take a screenshot of the client talking to the server and of it timing out when the server is stopped. Photo
  6. 6
    Watch the connection in tcpdump or Wireshark and point out the handshake. Capture on the loopback interface (lo on Linux, lo0 on a Mac), which needs sudo, and find the SYN, SYN-ACK and ACK before your first byte. Then find your bytes going each way. Take a screenshot of the capture with the three handshake packets marked. Photo