Skip to content

Nitworking Doc

ShaderHex edited this page Jan 11, 2025 · 4 revisions

Nitworking

Nitworking is a simple socket server library that facilitates the creation of a basic HTTP server. It supports both Windows and Unix-like systems. This library enables you to handle client requests, serve HTML files, and manage socket connections.

Overview

Nitworking provides easy-to-use functions for socket creation, connection handling, and file serving. It abstracts platform-specific code for Windows and Linux/macOS systems. With Nitworking, you can set up a basic HTTP server to serve web pages dynamically.


Setup

Required Libraries

  • On Windows, you need winsock2.h and ws2tcpip.h.
  • On Unix-like systems (Linux, macOS), sys/socket.h, netinet/in.h, unistd.h, string.h, and arpa/inet.h are required.
  • The library requires a C++11-compatible compiler.

Installation

Option 1: Manually Adding the Header File

  1. Download or clone the Nitworking repository:
    git clone https://github.com/ShaderHex/Nitworking
  2. Copy the nitworking.h file into your project directory:
    cp Nitworking/src/nitworking.h /path/to/your/project/
  3. Include it in your project source files:
    #include "nitworking.h"

Functions

initialize_winsock()

This function initializes Windows sockets. It is necessary only on Windows platforms.

create_server_socket()

This function creates a server socket and configures it for connection acceptance:

  • Returns a socket descriptor (sock).
  • On Windows, INVALID_SOCKET is returned on failure.
  • On Unix-like systems, -1 is returned on failure.

bind_socket(int server_fd, const std::string& ip_addr, int port_input)

Binds the server socket to the specified IP address and port.

listen_for_connections(int server_fd)

Prepares the server to listen for incoming client connections.

accept_connection(int server_fd)

Accepts an incoming connection from a client and returns the client socket descriptor.

get_request_path(const char* request)

Extracts the request path from the raw HTTP request.

html_buffer(int client_fd, const char* html_code, PathMapping* mappings, int num_paths)

Handles the client's request and serves the appropriate HTML response.

change_buffer_size(int set_buffer_size)

Allows dynamic modification of the buffer size for reading requests.

html_from_file(const char* path_to_html)

Reads the HTML file from disk and returns its content as a string. Returns nullptr on error.

close_socket(int socket_fd)

Closes a socket (either client or server) and cleans up resources.


Structure: PathMapping

struct PathMapping {
    const char* path;  // The request path.
    const char* value; // The response HTML or file content.
};

Example

Example Code

Below is an example of using the Nitworking library to set up a simple server that serves a few HTML pages on specific routes.

#include "nitworking.h"
#include <iostream>
#include <map>

int main() {
#ifdef _WIN32
    initialize_winsock();
#endif

    PathMapping mapping[] = {
        {"/home", html_from_file("example.html")},
        {"/about", "<html><body>Made by ShaderHex (on github)</body></html>"},
        {"/contact", "<html><body>This project doesn't have a discord server apparently</body></html>"}
    };

    sock server_fd = create_server_socket();
    unsigned int port = 8080;

    bind_socket(server_fd, "127.0.0.1", port);

    listen_for_connections(server_fd);
    std::cout << "Listening on port " << port << std::endl;

    while (true) {
        sock client_fd = accept_connection(server_fd);
        
        std::cout << "Mapping 1 path: " << mapping[1].path << std::endl;
        
        html_buffer(client_fd, html_from_file("example.html"), mapping, 3);
        
        close_socket(client_fd);
    }

    close_socket(server_fd);
}

Steps in Example:

  1. Initialize Winsock (on Windows only).
  2. Define Path Mappings: An array of paths and their corresponding HTML responses. The server will serve static content for the defined paths.
  3. Create Server Socket: Creates a TCP/IP server socket to listen for client connections.
  4. Bind Server Socket: Bind the server to 127.0.0.1 on port 8080.
  5. Listen for Connections: Start listening for incoming client connections.
  6. Handle Connections: For each client, the server accepts the connection, maps the requested path to a response, and sends back the HTML content.
  7. Close Connections: After handling the request, the server closes the client socket and waits for the next one.

Compilation and Running

To compile the example, make sure to link with the necessary libraries:

  • On Windows: -lws2_32.
  • On Linux/macOS: No specific libraries needed for networking, but make sure -pthread is used if threads are involved.

Compile on Windows:

g++ -o server example.cpp -lws2_32

Compile on Linux/macOS:

g++ -o server example.cpp

Run the server:

./server

Now you can access the server by visiting http://127.0.0.1:8080/home, /about, and /contact in your browser.


Notes

  • Ensure that your firewall settings allow the port you are binding (e.g., 8080).
  • The example assumes basic error handling. You might need to extend error management in real production use cases.

License

Nitworking is released under the MIT license. See LICENSE for details.