This is an example workspace demonstrating various aspects of how to use Berkeley Sockets (in UNIX or UNIX-like operating systems such as Linux or macOS) or Windows Sockets in C++ programs. It was designed to accompany the video, Tutorial: Using Sockets In C++.
The Sockets
library provides the foundations of encapsulating in C++ classes
the Berkeley Sockets or Windows Sockets operating system Application
Programming Interface (API) for accessing a computer network. It contains
three externally available classes:
ClientSocket
represents a connection-oriented socket (i.e. TCP connection) used to connect a client to a remote server.ServerSocket
represents a connection-oriented socket (i.e. TCP connection) used to listen as a server for incoming connections from clients.DatagramSocket
represents a datagram-oriented socket (i.e. UDP endpoint) which can be used to send and receive datagrams on the network.
The Receiver
and Sender
programs accompany the DatagramSocket
class and
demonstrate how to send and receive datagrams.
The Client
program accompanies the ClientSocket
class and demonstrates how
to make outgoing connections as a client to a remote server.
The Server
program accompanies the ServerSocket
class and demonstrates how
to set up a socket for accepting incoming connections from remote clients as a
server.
Internally, the Sockets
library also includes the following classes, which
are used to handle various tasks in socket programming:
UsesSockets
is a class included in the compositions of other classes which use the operating system's socket APIs. Its responsibility is to acquire and release, when appropriate, operating system resources required to use the socket APIs correctly. It's important for the Windows implementation especially because it handles calling theWSAStartup
andWSACleanup
functions which must be called before using the API and after using the API (respectively).SocketEventLoop
is a class included in the compositions of other classes which require an event loop to operate a socket. It runs a worker thread to monitor the socket and trigger operations whenever the socket appears ready for them.Connection
is a class used by the implementations of both theClientSocket
andServerSocket
classes in order to asynchronously handle the reading and writing of data for a socket.PipeSignal
is a class which provides a file handle which can be provided to theselect
function in order to synchronize one thread with another. While one thread waits on the handle usingselect
, another thread can call theSet
method provided by the class instance to wake up the first thread. It's implemented using an operating system pipe.
This is a collection of portable C++11 libraries and programs which depends on the C++11 compiler and standard C and C++ libraries. It should be supported on almost any platform. The following are recommended toolchains for popular platforms.
- Windows -- Visual Studio (Microsoft Visual C++)
- Linux -- clang or gcc
- MacOS -- Xcode (clang)
This project can stand alone or be included in larger projects. CMake files are included for your convenience to generate a build system to compile the source code and link them into programs you can run.
There are two distinct steps in the build process using CMake:
- Generation of the build system, using CMake
- Compiling, linking, etc., using CMake-compatible toolchain
- CMake version 3.8 or newer
- C++11 toolchain compatible with CMake for your development platform (e.g. Visual Studio on Windows)
Generate the build system using CMake from the solution root. For example:
mkdir build
cd build
cmake -G "Visual Studio 15 2017" -A "x64" ..
Either use CMake or your toolchain's IDE to build. For CMake:
cd build
cmake --build . --config Release
Each example program will be built under a subdirectory of the build folder.
For example, on Windows, the file build/Server/Server.exe
will be built
if the build directory was named build
as in the above example.