C++ sockpp Introduction














































C++ sockpp Introduction



What is SOCKPP?

It is modern and simple in use C++ socket library that provides the connection between client and server application parts.

This is a fairly low-level C++ library that includes socketacceptor, and connector classes that are familiar concepts from other languages.

The base socket class wraps a system socket handle, and maintains its lifetime. When the C++ object goes out of scope, it closes the underlying socket handle. Socket objects are generally moveable but not copyable. A socket can be transferred from one scope (or thread) to another using std::move().

 

Currently supports: IPv4, IPv6, and Unix-Domain Sockets on Linux, Mac, and Windows.

Requirements:

A conforming C++-14 compiler.
  • gcc v5.0 or later (or) clang v3.8 or later.
  • Visual Studio 2015, or later on WIndows.
CMake v3.5 or newer.
Doxygen (optional) to generate API docs.
Catch2 (optional) to build and run unit tests.

Build system used: CMake.

Namespace used: sockpp namespace.

 

For Linux users: Set the standard targets to build and install this library, and then run ldconfig to insure that the loader cache is updated:

 

  $ git clone https://github.com/fpagliughi/sockpp.git

  $ cd sockpp

  $ make

  $ sudo make install

  $ sudo ldconfig

 

Advantages:

  •   When a connection is accepted, a new, streaming socket is created. That new socket can be handled directly or moved to a thread (or thread pool) for processing.
  •   When connected, the socket is a streaming one, which can be used to read and write, directly.
  •   The TCP client is somewhat simpler in that a tcp_connector object is created and connected, then can be used to read and write data directly.
  •   UDP sockets can be used for connectionless communications.
  •   socket can be moved from one thread to another safely. This is a common pattern for a server which uses one thread to accept incoming connections and then passes off the new socket to another thread or thread pool for handling.
  •   A socket can be moved.
  •   The socket::shutdown() method can be used to communicate the intent to close the socket from one of these objects to the other without needing another thread signaling mechanism.

 

Disadvantages:

  •   A socket object is not thread-safe. Applications that want to have multiple threads reading from a socket or writing to a socket should use some form of serialization, such as a std::mutex to protect access.
  •   The default constructors for each of the socket classes do nothing, and simply set the underlying handle to INVALID_SOCKET. They do not create a socket object. The call to actively connect a connector object or open an acceptor object will create an underlying OS socket and then perform the requested operation.
  •   A socket cannot be copied.


More Articles of Julia Vanyushkina:

Name Views Likes
C++ sockpp Introduction 781 1

Comments