ruoka / net4cpp

Network library implemented with C++20 standard

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

net4cpp

Network library based on C++20 standard

Echo Server Example

try
{
    auto ator = acceptor{"::1", "2112"}; // IPv6 localhost
    auto [stream,client,port] = ator.accept();

    while(stream)
    {
        auto echo = ""s;
        getline(stream, echo);
        stream << echo << endl;
        clog << echo << endl;
    }
}
catch(const exception& e)
{
    cerr << "Exception: " << e.what() << endl;
}

Http Request Example

try
{
    auto s = connect("http://www.google.com");

    s << "GET / HTTP/1.1"                << crlf
      << "Host: www.google.com"          << crlf
      << "Connection: close"             << crlf
      << "Accept: text/plain, text/html" << crlf
      << "Accept-Charset: utf-8"         << crlf
      << crlf
      << flush;

    while(s)
    {
        auto c = ' ';
        s >> noskipws >> c;
        clog << c;
    }
    clog << flush;
}
catch(const exception& e)
{
    cerr << "Exception: " << e.what() << endl;
}

Syslog Stream Example

#include "net/syslogstream.hpp"

[...]

slog.level(syslog::severity::info);
slog.facility(syslog::facility::local0);
slog.tag("example");

auto clothes = "shirts"s; auto spouse = "wife"; auto wrong = false;

slog << debug   << "... " << 3 << ' ' << 2 << ' ' << 1 << " Liftoff" << flush;

slog << info    << "The papers want to know whose " << clothes << " you wear..." << flush;

slog << notice  << "Tell my " << spouse << " I love her very much!" << flush;

slog << warning << "Ground Control to Major Tom Your circuit's dead, there's something " << boolalpha << wrong << '?' << flush;

slog << error   << "Planet Earth is blue and there's nothing I can do." << flush;

About

Network library implemented with C++20 standard

License:MIT License


Languages

Language:C++ 95.5%Language:Makefile 3.5%Language:Dockerfile 1.0%