DEV Community

Cover image for I made my own C++ networking library at 16: here's how.
Mehdi BR
Mehdi BR

Posted on

I made my own C++ networking library at 16: here's how.

The Problem

Around a week ago, I wanted to focus on networking to start working on a home lab project. During this, I thought to myself: "Are there any simple libraries out there?" So I took to searching, I only found big libraries like Boost.Asio that are way too overkill for my needs, and honestly, too hard for me, or any beginner/intermediate developer, to understand. So I decided to make my own simple library, NovusNet, focused towards people of the same niche as me, people that don't need the complexity of larger libraries.

The idea

Before I even started coding, I sat down and thought to myself: "What do I want this library to be?" Simple, I needed it to be easy to understand, not have complex stuff that you don't need, and overall be as beginner friendly as possible without making it slow and unsafe.

The build

With the keyboard at my fingertips, I began coding, it was super simple at first, just setting up sockets and shoving them into functions. But as hours went by, I started to want more features. I dove into threads to start multi-client handling, which alone took me about 2 days of straight coding. I dove into OpenSSL to encrypt every connection, which took me around a day of googling new stuff. All that led the project to where it is now.
Setting up networking has never been so easy:

Server-side

#include "nn.hpp"
#include <iostream>
#include <chrono>

int main(){
    runServer(9090);
    //"onMessage" returns clientN and msg of any received message from any client.
    onMessage([](int clientN, std::string msg){
        //more detailed logic can go on here depending on what you wanna do.
        std::cout << "Client " << clientN << ": " << msg << "\n";
    });
    while(true){
        std::this_thread::sleep_for(std::chrono::milliseconds(100));
    }
}
Enter fullscreen mode Exit fullscreen mode

Client-side

#include "nn.hpp"
#include <iostream>

int main(){
    std::string msg;
    //runClient(ip,port) connects to a server
    int client = runClient("127.0.0.1", 9090);
    //receive client id and assign it for later use
    msg = recvMsg(client);
    int clientFD = std::stoi(msg);
    std::cout<<clientFD<<'\n';
    while(true){
        std::getline(std::cin,msg);
        //sendMsg(string msg) sends data as a string
        sendMsg(msg,1);
    }
    return 0;
}
Enter fullscreen mode Exit fullscreen mode

Results

NovusNet is now far easier to setup than any other pure c++ networking library. The average person making a home lab or a simple multiplayer game never needed a library as big as the ones currently available, they just needed something easy, fast, and secure.

What I learned

Working on this project taught me a lot about callbacks, lambdas, threads, and sockets. It also taught to make my own solution when I can't find someone else's.

Overall

I'm 16 and this is my first major project along with some other smaller ones on GitHub. If you're a beginner who believes networking is out of reach, it's not. You can find my project at NovusNet. If you wanna support me, go check it out and star it!

Top comments (0)