-
Notifications
You must be signed in to change notification settings - Fork 0
Nitworking Doc
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.
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.
- On Windows, you need
winsock2.handws2tcpip.h. - On Unix-like systems (Linux, macOS),
sys/socket.h,netinet/in.h,unistd.h,string.h, andarpa/inet.hare required. - The library requires a C++11-compatible compiler.
- Download or clone the Nitworking repository:
git clone https://github.com/ShaderHex/Nitworking
- Copy the
nitworking.hfile into your project directory:cp Nitworking/src/nitworking.h /path/to/your/project/
- Include it in your project source files:
#include "nitworking.h"
This function initializes Windows sockets. It is necessary only on Windows platforms.
This function creates a server socket and configures it for connection acceptance:
- Returns a socket descriptor (
sock). - On Windows,
INVALID_SOCKETis returned on failure. - On Unix-like systems,
-1is returned on failure.
Binds the server socket to the specified IP address and port.
Prepares the server to listen for incoming client connections.
Accepts an incoming connection from a client and returns the client socket descriptor.
Extracts the request path from the raw HTTP request.
Handles the client's request and serves the appropriate HTML response.
Allows dynamic modification of the buffer size for reading requests.
Reads the HTML file from disk and returns its content as a string. Returns nullptr on error.
Closes a socket (either client or server) and cleans up resources.
struct PathMapping {
const char* path; // The request path.
const char* value; // The response HTML or file content.
};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);
}- Initialize Winsock (on Windows only).
- Define Path Mappings: An array of paths and their corresponding HTML responses. The server will serve static content for the defined paths.
- Create Server Socket: Creates a TCP/IP server socket to listen for client connections.
-
Bind Server Socket: Bind the server to
127.0.0.1on port8080. - Listen for Connections: Start listening for incoming client connections.
- Handle Connections: For each client, the server accepts the connection, maps the requested path to a response, and sends back the HTML content.
- Close Connections: After handling the request, the server closes the client socket and waits for the next one.
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
-pthreadis used if threads are involved.
g++ -o server example.cpp -lws2_32g++ -o server example.cpp./serverNow you can access the server by visiting http://127.0.0.1:8080/home, /about, and /contact in your browser.
- 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.
Nitworking is released under the MIT license. See LICENSE for details.