Skip to content

About

An easy to use C++17 POSIX socket and networking library for Linux. RAII, TCP/UDP, IPv4/IPv6, multicast, Unix-domain sockets, SocketCAN, blocking I/O with timeouts, and an epoll-based event loop.

Topics

Resources

Stars

32 stars

Watchers

5 watching

Forks

Latest commit

 

History

406 Commits

Folders and files

Repository files navigation

simple-socket - modern C++ sockets

C++17 Linux License CMake Documentation Build Release

simple-socket is a lightweight C++17 based wrapper around POSIX socket APIs. It keeps the native socket model visible while providing RAII ownership, error handling, poll and convenience functions. The API uses strings for IPv4/6 addresses and keeps pointer handling to a minimum. Blocking socket operations have variants accepting a timeout. Switching to readiness-based non-blocking I/O is fully supported by the same interface. The included Event Loop lets you handle multi-socket I/O and timers in a single thread. Functions for DNS resolution and network interface discovery and management are included.

Designed for Linux applications, embedded systems, inter-process communication (IPC) and network services that need direct control over sockets without the complexity of a complete networking framework.

Features

  • IPv4 / IPv6 TCP and UDP sockets
  • Unix-domain stream, seqpacket and datagram sockets
  • UDP multicast support (IPv4 only)
  • Blocking and Non-Blocking I/O using the same API
  • Timeout support for connect, accept and receive (poll)
  • Event-driven I/O with timer support
  • Single-threaded handling of multiple sockets
  • Peer-disconnect detection for connected sockets
  • Native descriptor adoption and duplication (e.g. socket activation)
  • Hostname and service resolution
  • Network-address and interface utilities
  • Optional SocketCAN support
  • Experimental SCTP support
  • CMake build, unit tests and example programs

Design choices

  • RAII socket ownership with minimal internal state
  • Consistent string-based interface for all address types
  • Expected network failures return IOResult or ConnectResult instead of throwing
  • Unexpected syscall errors are checked and throw SocketException
  • Transient failures (e.g. EINTR, accept retries) are handled internally
  • Created and accepted sockets have the close-on-exec flag set

Documentation

Example

Minimal TCP server:

#include <simple_socket/TCPSocket.h>
#include <iostream>

int main()
{
    try
    {
        NET::TCPSocket server;
        server.setReuseAddress(true);
        server.bind("127.0.0.1", 47777);
        server.listen();

        std::cout << "Waiting for a client...\n";

        NET::TCPSocket client = *server.accept();

        auto sent = client.sendAll("hello from simple-socket");

        if (sent.status == NET::IOStatus::PeerDisconnected)
        {
            std::cerr << "Client disconnected.\n";
            return 1;
        }

        std::cout << "Successfully sent " << sent.bytes << " bytes.\n";
        return 0;
    }
    catch (const NET::SocketException& e)
    {
        std::cerr << e.what() << '\n';
        return 1;
    }
}

Event loop:

#include <simple_socket/EventLoop.h>
#include <iostream>

using namespace std::chrono_literals;

int main()
{
    NET::EventLoop events;

    events.runEvery( 1s, [] {
        std::cout << "Heartbeat\n";
    });

    events.runAfter( 5s, [&] {
        events.stop();
    });

    events.run();
}

Building

To use the library in your own project, it is easiest to use add_subdirectory in your CMakeLists.txt:

add_subdirectory(external/simple-socket)

add_executable(myapp main.cpp)
target_link_libraries(myapp PRIVATE simple-socket)

When used as a subproject, simple-socket does not build tests or examples by default. For a standalone build with unit tests, install CppUnit and use:

cmake -B build
cmake --build build
ctest --test-dir build --output-on-failure

SocketCAN support can be enabled with -DSIMPLE_SOCKET_ENABLE_CAN=ON. Experimental SCTP support with -DSIMPLE_SOCKET_ENABLE_SCTP=ON. API documentation can be generated with the doc target when Doxygen is available.

License

This library is released under the MIT License. See LICENSE.

About

An easy to use C++17 POSIX socket and networking library for Linux. RAII, TCP/UDP, IPv4/IPv6, multicast, Unix-domain sockets, SocketCAN, blocking I/O with timeouts, and an epoll-based event loop.

Topics

Resources

Stars

32 stars

Watchers

5 watching

Forks

Releases

Used by

Contributors

Languages