#Skip to menu

Golang: Echo Protocol (RFC862)


The Echo Protocol is one of the simplest possible internet protocols. The server listens on port 7 and any data that is read is sent back to the client. It can run either over TCP or UDP. In this article we’ll see how to implement a server and a client for the echo protocol in golang.

The Server (TCP)

First let’s see the server over TCP. Create a file named echoservertcp.go, open it in your favorite editor, and add the following lines:

We import the needed packages and then set the log flags to Lshortfile to better spot errors when they happen. We then need to establish a TCP connection on localhost port 7. Add net to the import list, and then add these lines to main:

Now we need to listen for all the incoming connections and reply to each one. We’ll use a goroutine to process each connection so as to make our server concurrent:

echo simply reads all the data from the client and sends it back to the client, we do this with the handy io.Copy function (don’t forget to import io):

We’re done. Here is the final result (echoservertcp.go):

The Client (TCP)

The client is simple as well. We:

  1. open a connection to localhost port 7
  2. read from stdin until EOF and send everything over the connection
  3. read the response and write it to stdout.

As simple as this is, there’s one little gotcha. After writing the data, we need to tell the server that we have nothing more to write, otherwise it will block. So we check (trought type assertion) that our connection is effectivly a TCP connection and if it is so we close the writing end. Here’s the code (echoclienttcp.go):

Let’s try it!

Build the server and run it:

$ go build echoservertcp.go
$ ./echoservertcp
echoservertcp.go:21: listen tcp :7: bind: permission denied

We get an error because all ports below 1023 are reserved and can only be accessed by root (see list of ports). So you’ll need to login as root trought su or sudo and rerun the command:

# ./echoservertcp

In another terminal build and run the client:

$ go build echoclienttcp.go
$ echo 'Hey, GNU rocks!' | ./echoclienttcp
Hey, GNU rocks!

Hey, it works!

The Server (UDP)

In UDP we work with datagrams. We don’t need to listen or accept any incoming connection. We read a datagram and send back the response. In the TCP code we block with net.Accept but here we’ll use a semaphore to limit the number of goroutines running. Here’s the code (echoserverudp.go):

The Client (UDP)

In the client we:

  1. read data from stdin
  2. send it with a datagram
  3. read the response
  4. and write it to stdout

Code (echoclientudp.go):

Get the source code


Please don’t use the echo protocol. It’s useless and what’s more it’s dangerous: see CA-1996-01. It’s appropriate only for learning purposes.

Great Golang ebook

Hey! I'm writing an ebook on how to write great Golang code. If it sounds interesting write to greatgo@xojoc.pw to be updated.

See all the discussions around the web about this page.