8th Week – Simple chat over TCP with Boost ASIO

Download the zip file

The goal of this task is to get familiar with the Boost ASIO library. You are going to implement a trivial chat based on TCP sockets. Do not get scared, there is not much code you have to write and the hardest part of the code is already prepared for you.


There is no special protocol for our chat. We take advantage of sockets being a streams and we simply send characters as they come. Our final application can work in two modes – a server and a client.

Server (the part we have already implemented for you) acts a hub. It listens for incoming TCP connections on a given port, accepts them and, if it receives data on a socket, it broadcasts them to all the other connected clients. It also provides a possibility for the user to enter a message and send it to everyone.

Client (the part you have to implement) on the other hand should try to open a TCP connection to server and when a connection is successfully established, it should provide to a user an option to send a message to server (and therefore, to send it to all connected clients) and also prints on screen everything it receives from the server.

Note that communication over network is an IO operation, which is not performed right away and may introduce lags. We are also doing two things at the same time – we wait for user input and we wait for data from socket. In this case, asynchronous handling of the sockets might come handy.

Your task

We have prepared two classes for you. You can look at their source code, take inspiration from it, modify it as you wish or even make bug fixes. The code is not bullet proof, as we try to show you the basic of Boost ASIO in simplest possible form.

Your task is to implement class Client and function runClient. You can take inspiration from the Server class. Do not hesitate to use Boost ASIO documentation, namely we recommend starting with overview of TCP. If understanding the basics from the official documentation is hard for you, we can recommend this article (beware it uses an older version of the Boost library, so there might be small differences).

How to run it

Resulting application should run in two modes – server and client. If you run one instance of ./chat server localhost 33333 and two instances of ./chat client localhost 33333, you should be able to chat between these two instances. If you would like to test the server and you do not have implementation for the client, you can simulate client using nc localhost 33333. If you want to simulate server, you can use nc -l -p 33333.


If the task was too easy for you, you can extend the chat by introducing a message format. This allows you to implement e.g. client identification (nicknames), “XY is typing” messages and so on.


The solution is available.

7th Week – Threading

Threads in GDB quick guide

Parallel queue

Your task this week is to implement a simple thread-safe queue and in doing so get familiar with the basic synchronization primitives from the C++ standard library.

Basic task

Implement class Queue< T > as thread-safe wrapper for std::deque using mutexes and condition variables. It should support following operations:

Write also a simple multi-threaded program which tests the capabilities of your queue (multiple readers/writers). Before you start, it might be useful to read the relevant parts of cppreference.

Bonus task

When using blocking operations (such as pop) it is often useful to be able to interrupt blocked threads. Add a support for queue shutdown:

Please note that the notion of “future” is somewhat vague in parallel programming. E.g. it is admissible that a pop which is executed in parallel with kill will return a value if the queue is not empty and it is also admissible it will throw an exception. It is, however, not admissible it will block.


The solution is available.

6th Week

There are two tasks for this week: a pretty-printer library using templates and SFINAE-based code for deriving operator + from operator += (in the same spirit as used for operator != and operator == in the lecture. Furthermore, you are encouraged to read lecture codes and ask if anything is not clear to you.

5th Week

Download the zip file

4th Week

3rd Week

2nd Week

1st Week