Socket programming examples in C++
Clone or download
Latest commit e9c726b Sep 17, 2016
Type Name Latest commit message Commit time
Failed to load latest commit information.
echo Improved Makefile Sep 17, 2016
simple-echo Fixed the slow server by adding a sleep Sep 8, 2015
tests Added test file. Sep 15, 2015
unix-echo Minor changes to comments in Makefile Sep 10, 2014
.gitignore Added an echo server and client based on UNIX domain sockets. Sep 25, 2013
LICENSE Initial commit Sep 10, 2013 Reorganized the code. Sep 9, 2014

Socket programming examples in C++

Simple examples that show how to do socket programming in C++. Use


to compile the code in each directory.

Simple Echo Server and Client

The files simple-echo/ and simple-echo/ contain a basic echo server and client. The server handles only one client at a time and simply sends back to the client whatever it receives.

Run server with client and see what happens.

Caution: both the client and the server do not properly check the return values of send() and recv(). This means that long messages may not be echoed properly. Better code is in subsequent examples. This code is here just to show the basics.

Slow Echo Server

To illustrate the poor quality of the code in the simple echo server and client, the file simple-echo/ is an echo server that sends replies only one character at a time. While this is not how any server is written, it illustrates what can happen when sending larger messages across the network. Those messages can be split into smaller pieces by TCP, so the a developer cannot assume that a single call to recv() will get the entire message.

Run slow-server with client and see what happens.

Echo Server and Client

This code implements a more functional and complete echo server and client. The client sends messages terminated by a newline, and the server parses until it receives a newline. Both client and server check the return value of send() and recv() and use loops to be sure the entire message is sent or received in each case.

This code is located in the echo directory. The main programs are in and The server uses and inet-server.h to create Internet sockets using IPv4 addresses. These files in turn rely on and server.h for the protocol logic and processing. The client uses and inet-client.h to create the client version of an Internet socket. These files likewise rely on and client.h for the protocol processing.

UNIX Echo Server and Client

This code converts the echo client and server to use UNIX sockets instead of Internet sockets. UNIX sockets use the file system to identify the socket, rather than IP addresses and ports. In this code, we use /tmp/unix-socket as the name of the socket.

By using a UNIX socket, this code is restricted to communication with local processes only. In addition, it is not portable to operating systems that do not support UNIX sockets. This code is tested only on Linux, but most of it should be portable to BSD and Mac OS X, with some small changes.

This code is located in the unix-echo directory. The server uses,, and unix-server.h. These latter two files derive a class based on and socket.h. The client uses,, and unix-client.h. Likewise, these latter two files derive a class based on and client.h.

Note that this code reuses the same base classes in socket.h and client.h, showing how you can swap out a different type of socket, but the protocol logic and processing remains the same.