Build Your Own Echo Server
This challenge is to build your own echo server. An echo server is a server that implements the Echo Protocol, as defined in RFC 862. The short version of the protocol is this: the server echoes back to the originating client any data it receives until the connection is terminated.
In case you’re wondering, this is a useful debugging and measurement tool for network engineers. It’s also a great first server to build if you’re learning about network programming and interested in eventually building your own Redis, building your own Memcached or building your own server.
By building your own echo server you’ll learn about:
- Using the sockets API of your programming language, specifically how to open a socket, bind it to an address and port, listen for incoming connections and then send and receive data over the network.
- TCP and UDP network protocols.
- How Internet standards are defined.
The Challenge - Building An Echo Server
For this Coding Challenge your goal is to build an echo server that implements all of the Echo Protocol as defined in RFC 862.
Step Zero
As usual this is where you select the programming language you’re going to use for this challenge, set up your IDE and grab beverage of your choice.
Once you’ve done that have a read of the RFC (if you haven’t already) and check if you already have netcat or telnet installed on your system. If not install one or both of them, they’re very useful tools for debugging network servers.
Step 1
In this step your goal is to build a simple server that will start-up, bind to all the local IP addresses, listen on port 7, and accept a TCP connection. To complete this step simply have the server print out a log message to show a connection has been accepted and then have it shutdown. Refer to the documentation for you programming language to find out how to write network programs using it.
You can test your server using two terminals, in one run the server and in the other use netcat or telnet to connect to it. That looks like this:
# Server Terminal
% ccecho
Accepted connection from: 127.0.0.1:63798
%
# Client terminal, using netcat
% nc localhost 7
Step 2
In this step your goal is to extend your server to accept multiple concurrent connections. This will require you to keep the main ‘thread’ of execution running and listening for incoming connections as well as spawning a new ‘thread’ of execution to handle each client.
Note I’m putting ‘thread’ in quotes here because I’m referring to something that is running concurrently alongside something else. That could be multiple operating system threads or it could be multiple async tasks. Your choice!
You can test your server using two terminals, in one run the server and in the other use netcat or telnet to connect to it. That looks like this:
# Server Terminal
% ccecho
Accepted connection from: 127.0.0.1:63243
Accepted connection from: 127.0.0.1:63245
Note the server stays running now.
Then use two other terminals to connect like this:
# Client terminal, using netcat
% nc localhost 7
Step 3
In this step your goal is to read data from the client and write that data back to the client. That should continue until the client terminates the connection.
That will look like this:
# Server Terminal
% ccecho
Accepted connection from: 127.0.0.1:53684
Client disconnected
%
# Client terminal, using netcat
% nc localhost 7
Hello, Coding Challenges
Hello, Coding Challenges
^C%
Step 4
In this step your goal is to add a command line flag so your echo server can be started up using either TCP or UDP on port 7.
# Server Terminal
% ccecho -udp
UDP Echo server listening on :7
^C%
# Client terminal, using netcat -u for UDP, your version may differ
% nc -u localhost 7
Hi Coding Challenges
Hi Coding Challenges
^C%
Step 5
In this step your goal is to shutdown cleanly. Ensuring all connections are closed and any inflight echo messages are sent before shutdown.
Going Further
After you’ve built your own echo server you might like to try:
- Building your own Web Server
- Building your own Redis
- Building your own Memcached
- Building your own Load Balancer
All of which will introduce you to building different aspects of a network server.
Help Others by Sharing Your Solutions!
If you think your solution is an example other developers can learn from please share it, put it on GitHub, GitLab or elsewhere. Then let me know - ping me a message on the Discord Server, via Twitter or LinkedIn or just post about it there and tag me. Alternately please add a link to it in the Coding Challenges Shared Solutions Github repo.
Get The Challenges By Email
If you would like to receive the coding challenges by email, you can subscribe to the weekly newsletter on SubStack here: