How to do it...

We'll develop two programs, a client and a server. The server will start and listen on a specific port that is ready to accept an incoming connection. The client will start and connect to the server identified by an IP and a port number:

  1. With the Docker image running, open a shell and create a new file, clientTCP.cpp. Let's add some headers and constants that we'll need later:
#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <string.h>#include <sys/types.h>#include <sys/socket.h>#include <netinet/in.h>#include <netdb.h>#include <iostream>constexpr unsigned int SERVER_PORT = 50544;constexpr unsigned int MAX_BUFFER = 128;
  1. Let's start writing the main method now. We start by initializing socket and getting ...

Get C++ System Programming Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.