Skip to content
/ net Public

A light and simple cross-platform socket wrapper

License

Notifications You must be signed in to change notification settings

lionkor/net

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

net

CMake Linux Build CMake Windows Build

A light and simple cross-platform socket wrapper in C++20.

Here's a minimal TCP server example, which says "Hello, World!", then prints the answer, and exits. It also demonstrates binding to a specific IPv6 address (:: in this case).

#include <lk/net/Socket.h> // lk::net library
#include <iostream>

int main() {
    lk::net::TCPSocket socket;
    socket.bind(2233, "::");            // bind to localhost:2233
    socket.listen(10);                  // listen with a backlog of 10
    auto client = socket.accept();      // blocking accept
    std::string msg = "Hello, World!"; 
    auto n = client.write(msg);         // write takes any container
    if (size_t(n) != msg.size()) {
        // handle
    }
    std::string buf(64, ' ');
    n = client.read(buf);
    buf[n] = '\0';
    std::cout << "client said: " << buf << "\n";
}   // socket is closed via RAII