Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 5b9d10224c
Fetching contributors…

Cannot retrieve contributors at this time

37 lines (31 sloc) 1.03 kb
\begin{Exercise}[title={Echo server},difficulty=1]
\label{ex:echo}
\Question\label{ex:echo q1}
Write a simple echo server. Make it listen to TCP port number 8053 on localhost. It should
be able to read a line (up to the newline), echo back that line and then close the connection.
\Question\label{ex:echo q2}
Make the server concurrent so that every request is taken care of in a separate
goroutine.
\end{Exercise}
\begin{Answer}
\Question
A simple echo server might be:
\lstinputlisting[caption=A simple echo server]{ex-communication/src/echo.go}
When started you should see the following:
\vskip\baselineskip
\begin{display}
\pr \user{nc 127.0.0.1 8053}
\user{Go is *awesome*}
Go is *awesome*
\end{display}
\Question
To make the connection handling concurrent we \emph{only need to change one line} in our
echo server, the line:
\begin{lstlisting}
if c, err := l.Accept(); err == nil { Echo(c) }
\end{lstlisting}
becomes:
\begin{lstlisting}
if c, err := l.Accept(); err == nil { go Echo(c) }
\end{lstlisting}
\end{Answer}
Jump to Line
Something went wrong with that request. Please try again.