Skip to content

Repository files navigation

netcpp windowslinuxcodecovlangVcpkg packageLicense

[한국어]
netcpp is simple C++ network library. It supports windows and linux platform.

Installation

This library supports vcpkg port. If you had already installed vcpkg, You can install this package simply with the command line below.

vcpkg install netcpp # In classic mode
vcpkg add port netcpp # In manifest mode

Or clone this repository, and execute the command line below.

git clone https://github.com/index1207/netcpp.git &&cd netcpp # clone and move directory
cmake -B build # CMake Configuration
cmake --build build --config <BUILD_MODE># Build library
cmake --install build --config <BUILD_MODE> --prefix <PATH_TO_INSTALL># Install to other project

netcpp provides CMake targets:

find_package(netcppCONFIGREQUIRED)
target_link_libraries(mainPRIVATEnetcpp::netcpp)

CMake Options

OptionDescription
NETCPP_BUILD_SHAREDBuild by shared library
NETCPP_TESTInclude unit test in build

Example

  • Create a socket
// <net/socket.hpp>
net::socket tcp_socket(net::protocol::tcp); // Create a TCP socket
net::socket udp_socket(net::protocol::udp); // Create a UDP socket
net::socket empty_socket;
empty_socket.create(net::protocol::tcp); // Create a new TCP socket
  • Async I/O
// <net/context.hpp>
net::socket sock(net::protocol::tcp); // Create a new TCP socket.
net::context connect_ctx; // A context that necessary to async I/O
connect_ctx.endpoint = ENDPOINT; // Specify the endpoint.
connect_ctx.completed = [](net::context* ctx, bool success) { // callbackif (success)
{
std::cout << "Connected" << std::endl;
}
else
{
std::cout << "Failed to connect" << std::endl;
}
};
sock.connect(&connect_ctx); // Connect to specified endpoint asynchronously.
  • Basic connection
// Server
#include<net/Socket.hpp>
#include<iostream>intmain()
{
net::native::initialize(); // Initialize Native API
net::socket sock(net::protocol::tcp); // Create new TCP socketif (!sock.is_open()) // Validate socketreturn -1;
if (!sock.bind(net::endpoint(net::ip_address::loopback, 8085))) // Bind the address `tcp://loopback:8085`return -1;
if (!sock.listen()) // Ready to acceptreturn -1;
while(true)
{
auto client = sock.accept(); // accept other client. it returns new client socket.
std::cout << "Connected\n";
}
}
// Client
#include<net/socket.hpp>
#include<iostream>intmain()
{
net::native::initialize(); // Initialize Native API
net::socket sock(net::protocol::tcp); // Create new TCP socketif (!sock.is_open()) // Validate socketreturn -1;
if (!sock.connect(net::endpoint(net::ip_address::loopback, 8085))) // Try to connect to server.return -1;
std::cout << "Connected!";
}
  • DNS
// <net/dns.hpp>net::dns::get_host_name() // get host's name
net::dns::get_host_entry("www.example.com") // get www.example.com's host entry
  • Exception
try {
if (!sock.connect(ENDPOINT))
thrownet::network_exception("connect()");
}
catch(std::exception& e) {
std::cout << e.what() << std::endl; // connect(): Cannot assign requested address. [10049]
}

Dependencies

OSLibrary
WindowsWinsock2
Linuxliburing

Contribute

The repository is whenever welcome any issues or PRs!

Minimum required compiler version

  • Windows
    • Visual Studio 2019
  • Linux
    • Clang 12
    • GCC 10

About

A simple cross-platform async socket library

Topics

Resources

Stars

11 stars

Watchers

1 watching

Forks

Releases

Used by

Contributors

Languages