It uses standard library functions with no other dependencies and provides a safer yet expressive version of original Berkeley Sockets API. Common mistakes are abstracted away giving a boost in productivity for library user.
#include "socket.hpp"
#include <iostream>
using namespace net;
int main()
{
try {
Socket s(Domain::IPv4, Type::TCP);
s.start("127.0.0.1", 24000);
while (true) {
const auto peer = s.accept();
const auto msg = peer.recv(15);
std::cout << msg << '\n';
}
} catch (std::exception &e) {
std::cerr << e.what() << '\n';
}
}
You don't need anything else other than a supported platform, a C++14 compiler and a few seconds to spare. The library is currently uses meson build system so first class dependency management is included for those using same. Otherwise just git clone
the library, and pass the includes and sources to your compiler and you're good to go.
Please take a look inside docs/
directory to find API and other documentation.
git clone https://github.com/c10k/net.git
cd net
mkdir build && cd build
meson .. && ninja # This will also create a dynamic lib
git clone https://github.com/c10k/net.git
cd net
mkdir build && cd build
meson .. && ninja
./test/testexe
You can see more examples under examples/
directory.
#include "socket.hpp"
#include <iostream>
using namespace net;
int main()
{
try {
Socket s(Domain::IPv4, Type::TCP);
s.start("127.0.0.1", 24000);
while (true) {
const auto peer = s.accept();
const auto msg = peer.recv(15);
std::cout << msg << '\n';
}
} catch (std::exception &e) {
std::cerr << e.what() << '\n';
}
}
#include "socket.hpp"
#include <iostream>
using namespace net;
int main()
{
try {
Socket s(Domain::IPv4, Type::TCP);
s.connect("0.0.0.0", 24000);
s.send("Hello World!");
} catch (std::exception &e) {
std::cerr << e.what() << '\n';
}
}
#include "socket.hpp"
#include <iostream>
#include <signal.h>
using namespace net;
int main()
{
try {
signal(SIGCHLD, SIG_IGN);
Socket s(Domain::IPv4, Type::TCP);
s.start("127.0.0.1", 24001);
while (true) {
const auto peer = s.accept();
if (!fork()) {
std::cout << peer.recv(10) << '\n';
return 0;
}
}
} catch (std::exception &e) {
std::cerr << e.what() << '\n';
}
}
- Proper namespacing in socket options
- Add more unit tests, increase coverage
- Add examples for non-blocking socket operations
- Add support for more protocols other than TCP, UDP, UNIX
- Add benchmarks against popular alternatives