{"id":19330593,"url":"https://github.com/alexandrerouma/net","last_synced_at":"2025-04-22T23:31:31.557Z","repository":{"id":104901910,"uuid":"546208939","full_name":"AlexandreRouma/net","owner":"AlexandreRouma","description":"Ultra lightweight cross-platform C++ networking library","archived":false,"fork":false,"pushed_at":"2024-01-26T16:14:33.000Z","size":80,"stargazers_count":8,"open_issues_count":0,"forks_count":3,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-02T04:47:20.649Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"lgpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/AlexandreRouma.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"license","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2022-10-05T18:03:41.000Z","updated_at":"2025-03-31T05:29:06.000Z","dependencies_parsed_at":null,"dependency_job_id":"889fcc10-083c-4f8d-affd-a9798cbdb873","html_url":"https://github.com/AlexandreRouma/net","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AlexandreRouma%2Fnet","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AlexandreRouma%2Fnet/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AlexandreRouma%2Fnet/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AlexandreRouma%2Fnet/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/AlexandreRouma","download_url":"https://codeload.github.com/AlexandreRouma/net/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250340216,"owners_count":21414498,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-10T02:37:13.449Z","updated_at":"2025-04-22T23:31:31.262Z","avatar_url":"https://github.com/AlexandreRouma.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Basic Usage\n\nFor detailed information, check the header. Everything is documented in javadoc format for easy autocompletion.\n\n## Adding to a project\n\nSimply copy the `networking.h` and `networking.cpp` files from the `lib` directory to a suitable place in your project.\n\n## Creating a TCP server\n\nHere is a simple \"echo\" server example\n\n```c++\n#include \u003cstdio.h\u003e\n#include \"networking.h\"\n\nint main() {\n    try {\n        // Listen on any IP on port 1234.\n        auto listener = net::listen(\"0.0.0.0\", 1234);\n\n        while (true) {\n            // Wait for a client\n            net::Address clientAddress;\n            auto client = client-\u003eaccept(\u0026clientAddress);\n\n            // Show info about the client in the console\n            char addrStr[128];\n            sprintf(addrStr, \"%s:%d\", clientAddress.getIPStr().c_str(), clientAddress.getPort());\n            printf(\"Connection from %s\\n\", addrStr);\n\n            while (true) {\n                // Read line or end connection on error/disconnect\n                std::string line;\n                if (client-\u003erecvline(line) \u003c= 0) {\n                    break;\n                }\n\n                // print line to console\n                printf(\"[%s] '%s'\\n\", line.c_str());\n\n                // Send back the line\n                client-\u003esendstr(line + '\\n');\n            }\n\n            // End connection just in case.\n            client-\u003eclose();\n        }\n    }\n    catch (std::runtime_error\u0026 e) {\n        fprintf(stderr, \"Error: %s\\n\", e.what());\n    }\n    return 0;\n}\n```\n\nHere to, you can alternatively give `net::listen` a `net::Address` instead of a host/port pair.\n\n## Connecting to a TCP server\n\nTo connect to a server, use the `net::connect()` function. On error, it throws an exception.\n\n```c++\n#include \u003cstdio.h\u003e\n#include \"networking.h\"\n\nint main() {\n    try {\n        // Connect to the server\n        auto client = net::connect(\"yourserver.com\", 1234);\n\n        // Send a few characters\n        client-\u003esendstr(\"Hello World!\\n\");\n\n        // Close the connection\n        client-\u003eclose();\n    }\n    catch (std::runtime_error\u0026 e) {\n        fprintf(stderr, \"Error: %s\\n\", e.what());\n    }\n    return 0;\n}\n```\n\nYou can alternatively give `net::connect` a `net::Address` instead of a host/port pair.\n\n## Create a UDP socket\n\nThis example sends a broadcast message, waits for a response and creates a new socket from it.\n\n```c++\n#include \u003cstdio.h\u003e\n#include \"networking.h\"\n\nint main() {\n    try {\n        // Create a UDP socket that sends on the broadcast address on port 1234.\n        auto sock = net::openudp(\"192.168.0.255\", 1234);\n\n        while (true) {\n            // Broadcast a message\n            sock-\u003esendstr(\"Hello! Is anyone here?\");\n\n            // Handle all responses\n            while (true) {\n                // Wait for a response or timeout after 1 seconds (1000 milliseconds) if nobody responds.\n                char msg[1024];\n                net::Address remoteAddr;\n                int len = sock-\u003erecv((uint8_t*)msg, sizeof(msg)-1, false, 1000, \u0026remoteAddr);\n                \n                // If we timed out, break out of the loop and send back the message again.\n                if (len \u003c= 0) {\n                    break;\n                }\n\n                // Add a null termination so we can print properly.\n                msg[len] = 0;\n\n                // Show the response.\n                printf(\"[%s:%d] '%s'\\n\", clientAddress.getIPStr().c_str(), clientAddress.getPort(), msg);\n            }\n        }\n    }\n    catch (std::runtime_error\u0026 e) {\n        fprintf(stderr, \"Error: %s\\n\", e.what());\n    }\n    return 0;\n}\n```\n\nAgain, you can alternatively give `net::listen` a `net::Address` instead of a host/port pair.\n\nThis next example acts as the listener for the example before.\n\n```c++\n#include \u003cstdio.h\u003e\n#include \"networking.h\"\n\nint main() {\n    try {\n        // Create a UDP socket with no particular destination since we'll specify a destination for each response. Listen on any IP and port 1234.\n        auto sock = net::openudp(Address(), \"0.0.0.0\", 1234);\n\n        while (true) {\n            // wait for a message\n            char msg[1024];\n            net::Address remoteAddr;\n            int len = sock-\u003erecv((uint8_t*)msg, sizeof(msg)-1, false, net::NO_TIMEOUT, \u0026remoteAddr);\n\n            // Send back directly to the server\n            sock-\u003esendstr(\"I'm here!\", \u0026remoteAddr);\n        }\n    }\n    catch (std::runtime_error\u0026 e) {\n        fprintf(stderr, \"Error: %s\\n\", e.what());\n    }\n    return 0;\n}\n```\n\n# Important notes\n\nThis project is still in development.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falexandrerouma%2Fnet","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falexandrerouma%2Fnet","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falexandrerouma%2Fnet/lists"}