Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Socket programming examples in C++
C++ Prolog
Branch: cs360-byu
Pull request Compare This branch is 17 commits ahead, 8 commits behind zappala:master.

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.

Socket programming examples in C++

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


to compile all the code using the supplied Makefile.

Simple Echo Server and Client

The files and 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 echo-server-simple with echo-client-simple 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 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 echo-server-slow with echo-client-simple and see what happens.

Echo Server and Client

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.

The main programs are in and The bulk of the server code is in and server.h. The bulk of the client code is in and client.h.

Something went wrong with that request. Please try again.