DEV Community

Cover image for Simple C++ networking libraries don't exist anymore - here's the fix.
Mehdi BR
Mehdi BR

Posted on

Simple C++ networking libraries don't exist anymore - here's the fix.

Recap

About 4 days ago, I posted a blog about my networking library, NovusNet, found here.

The problem

C++ networking libraries fall into 2 categories: Either too complex for simple use, or easy but unreliable. NovusNet fills a gap: Simple, secure, and fast.

What I added since then

Now, NovusNet offers a perfectly secure connection between 2 devices. Fully encrypted and limited access with passwords. Another big thing is the inclusion of NFTP, my own file transfer protocol, now integrated into the library. NFTP transfers any size file across the network safely and quickly.

Usage

Server-side:

#include "nn.hpp"
#include <iostream>
#include <chrono>
int main(){
    //runServer() now takes an additional variable: string password.
    runServer(9090, "YOUR_PASSWORD");
    // onMessage listens for any incoming message
    onMessage([](int clientN, std::string msg){
        // if the client announces a file is coming, receive it
        if(msg == "SENDING_FILE"){
            //recvFile() takes the download folder, and the client id.
            recvFile("Downloads", clientN); // saves to Downloads folder
        }
    });
    // keeps main() alive without tanking CPU
    while(true){
        std::this_thread::sleep_for(std::chrono::milliseconds(100));
    }
}
Enter fullscreen mode Exit fullscreen mode

Client-side:

// Client-side file sender
#include "nn.hpp"
#include <iostream>
int main(){
    // connect to server with ip, port, and password
    int client = runClient("127.0.0.1", 9090, "PassTest");
    // receive and store the clientID assigned by the server
    std::string msg = recvMsg(client);
    int clientID = std::stoi(msg);
    // announce to the server that a file is incoming
    sendMsg("SENDING_FILE", clientID);
    // send the file
    // sendFile() takes filepath and clientID assigned by the server.
    sendFile("image.png", clientID);
    return 0;
}
Enter fullscreen mode Exit fullscreen mode

Where it's going

As time goes, I'll update this library until it's perfect. I will move onto another project once this is done. Check it out on NovusNet and star it if you wanna support! If you want a clearer explanation, check NovusChat, it's a side project made purely on NovusNet to showcase the library's ability and simplicity.

Top comments (0)