The client logic

In this example, the following includes are needed for the client:

#include <array>#include <string>#include <iostream>#include <stdexcept>#include <unistd.h>#include <string.h>#include <sys/socket.h>#include <netinet/in.h>

Like the server, the client is created using a class to take advantage of RAII:

class myclient{    int m_fd{};    struct sockaddr_in m_addr{};public:

In addition to the class definition, two private member variables are defined. The first, like the server, is the socket file descriptor that will be used by the client. The second defines the address information for the server the client desires to communicate with.

The constructor of the client is similar to the server's, with some minor differences:

 explicit ...

Get Hands-On System Programming with C++ 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.