{"id":23055190,"url":"https://github.com/tomenz/socketlib","last_synced_at":"2025-08-15T05:31:59.461Z","repository":{"id":125723729,"uuid":"217572319","full_name":"Tomenz/SocketLib","owner":"Tomenz","description":"Socket / SSLSocket library for Windows/Linux (32/64)","archived":false,"fork":false,"pushed_at":"2024-11-13T12:30:26.000Z","size":366,"stargazers_count":7,"open_issues_count":0,"forks_count":4,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-11-13T12:34:40.491Z","etag":null,"topics":["cpp","cpp-library","cpp11","cpp14","ipv6","linux","openssl","socket-library","socketlib","sockets","ssl","thread","tls","windows"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Tomenz.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,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-10-25T16:19:55.000Z","updated_at":"2024-11-13T12:30:30.000Z","dependencies_parsed_at":null,"dependency_job_id":"eb0aafb5-e069-4c34-b8aa-48595a83b5eb","html_url":"https://github.com/Tomenz/SocketLib","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/Tomenz%2FSocketLib","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Tomenz%2FSocketLib/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Tomenz%2FSocketLib/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Tomenz%2FSocketLib/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Tomenz","download_url":"https://codeload.github.com/Tomenz/SocketLib/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":229893998,"owners_count":18140620,"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":["cpp","cpp-library","cpp11","cpp14","ipv6","linux","openssl","socket-library","socketlib","sockets","ssl","thread","tls","windows"],"created_at":"2024-12-16T01:11:19.708Z","updated_at":"2024-12-16T01:11:20.281Z","avatar_url":"https://github.com/Tomenz.png","language":"C++","readme":"[![Codacy Badge](https://app.codacy.com/project/badge/Grade/ad636ee5c24a4e8687fb43c4492f1188)](https://app.codacy.com/gh/Tomenz/SocketLib/dashboard?utm_source=gh\u0026utm_medium=referral\u0026utm_content=\u0026utm_campaign=Badge_grade)\n[![Build Status](https://travis-ci.org/Tomenz/SocketLib.svg?branch=master)](https://travis-ci.org/Tomenz/SocketLib)\n[![Build status](https://ci.appveyor.com/api/projects/status/ed2el9dnaua20hqj?svg=true)](https://ci.appveyor.com/project/Tomenz/socketlib)\n[![CMake](https://github.com/Tomenz/SocketLib/actions/workflows/cmake.yml/badge.svg)](https://github.com/Tomenz/SocketLib/actions/workflows/cmake.yml)\n[![C/C++ CI](https://github.com/Tomenz/SocketLib/actions/workflows/c-cpp.yml/badge.svg)](https://github.com/Tomenz/SocketLib/actions/workflows/c-cpp.yml)\n[![CodeQL](https://github.com/Tomenz/SocketLib/actions/workflows/codeql.yml/badge.svg)](https://github.com/Tomenz/SocketLib/actions/workflows/codeql.yml)\n\n# SocketLib\nSocket library written in c++11/14 for Windows/Linux (32/64)\n\n- IPv4 and IPv6 support\n- TCP and UDP support (both with SSL/TLS)\n- multi-cast support for IPv4 and IPv6\n- Enum all IP's on the host\n- notify if host ip comes up / changes / is removed\n- TLS 1.3 if openssl 1.1.1 or newer is used\n- Multi-threading, none blocking. All callback function executed in own thread\n\nExamples: https://github.com/Tomenz/Examples-SocketLib\n\nIn the Windows project files \"OpenSSL_HOME\" is stored for the \"include\" and \"library\" directories of openssl. You should create the enviroment variable, or change the include and library directories in the Visual Studio project files.\nIn Linux, libssl-dev must be installed (Or the source code of openssl must be compiled and installed).\n\nMeanwhile a short client / server example using the \"SocketLib\" library\u003cbr\u003e\n*** The \"SocketLib\" library in this example should by compiled with WITHOUT_OPENSSL defined, so we don't need the opensll library\u003cbr\u003e\n*** If you need SSL, you have to fix the path for the openssl headers and lib's when compiling the library\n\n```\n#include \u003cthread\u003e\n#include \u003cconio.h\u003e\n#include \u003csstream\u003e\n#include \u003ciomanip\u003e\n#include \u003ciostream\u003e\n\n#include \"SocketLib.h\"\n\nusing namespace std;\n\n#ifdef _DEBUG\n#ifdef _WIN64\n#pragma comment(lib, \"x64/Debug/socketlib64d\")\n#else\n#pragma comment(lib, \"Debug/socketlib32d\")\n#endif\n#else\n#ifdef _WIN64\n#pragma comment(lib, \"x64/Release/socketlib64\")\n#else\n#pragma comment(lib, \"Release/socketlib32\")\n#endif\n#endif\n\nvoid ServerThread(bool* bStop)\n{\n    TcpServer sock;\n\n    sock.BindErrorFunction([\u0026](BaseSocket* pSock) { cout \u003c\u003c \"Server: socket error\" \u003c\u003c endl; pSock-\u003eClose(); }); // Must call Close function\n    sock.BindCloseFunction([\u0026](BaseSocket*) { cout \u003c\u003c \"Server: socket closing\" \u003c\u003c endl; });\n\n    // This Callback is called if a new client connects to the server.\n    sock.BindNewConnection([\u0026](const vector\u003cTcpSocket*\u003e\u0026 lstSockets)\n        {\n            for (auto\u0026 pSocket : lstSockets)\n            {\n                if (pSocket != nullptr)\n                {\n                    pSocket-\u003eBindFuncBytesReceived([\u0026](TcpSocket* pTcpSocket)\n                        {\n                            size_t nAvailable = pTcpSocket-\u003eGetBytesAvailible();\n\n                            auto spBuffer = make_unique\u003cunsigned char[]\u003e(nAvailable + 1);\n\n                            size_t nRead = pTcpSocket-\u003eRead(\u0026spBuffer[0], nAvailable);\n\n                            if (nRead \u003e 0)\n                            {\n                                string strRec(nRead, 0);\n                                copy(\u0026spBuffer[0], \u0026spBuffer[nRead], \u0026strRec[0]);\n\n                                stringstream strOutput;\n                                strOutput \u003c\u003c pTcpSocket-\u003eGetClientAddr() \u003c\u003c \" - Server received: \"\n                                          \u003c\u003c nRead \u003c\u003c \" Bytes, \\\"\" \u003c\u003c strRec \u003c\u003c \"\\\"\" \u003c\u003c endl;\n\n                                cout \u003c\u003c strOutput.str();\n\n                                strRec = \"Server echo: \" + strRec;\n                                pTcpSocket-\u003eWrite(\u0026strRec[0], strRec.size());\n\n                                pTcpSocket-\u003eClose();\n                            }\n                        });\n                    pSocket-\u003eBindErrorFunction([\u0026](BaseSocket*) { cout \u003c\u003c \"Server: socket error\" \u003c\u003c endl; });\n                    pSocket-\u003eBindCloseFunction([\u0026](BaseSocket* pSock)\n                        {\n                            cout \u003c\u003c \"Server: socket closing\" \u003c\u003c endl;\n                        });\n                    pSocket-\u003eStartReceiving();\n                }\n            }\n        });\n\n\n    bool bCreated = sock.Start(\"0.0.0.0\", 3461);  // or \"::\" for IPv6\n\n    while (*bStop == false)\n    {\n        this_thread::sleep_for(chrono::milliseconds(10));\n    }\n\n    // Closing the server socket will not call the close callback\n    sock.Close();\n}\n\nvoid ClientThread(bool* bStop)\n{\n    TcpSocket sock;\n\n    sock.BindErrorFunction([\u0026](BaseSocket* pSock) { cout \u003c\u003c \"Client: socket error\" \u003c\u003c endl; pSock-\u003eClose(); }); // Must call Close function\n    sock.BindCloseFunction([\u0026](BaseSocket*) { cout \u003c\u003c \"Client: socket closing\" \u003c\u003c endl; });\n    sock.BindFuncBytesReceived([\u0026](TcpSocket* pTcpSocket)\n        {\n            size_t nAvailable = pTcpSocket-\u003eGetBytesAvailible();\n\n            auto spBuffer = make_unique\u003cunsigned char[]\u003e(nAvailable + 1);\n\n            size_t nRead = pTcpSocket-\u003eRead(\u0026spBuffer[0], nAvailable);\n\n            if (nRead \u003e 0)\n            {\n                string strRec(nRead, 0);\n                copy(\u0026spBuffer[0], \u0026spBuffer[nRead], \u0026strRec[0]);\n\n                stringstream strOutput;\n                strOutput \u003c\u003c pTcpSocket-\u003eGetClientAddr() \u003c\u003c \" - Client received: \"\n                          \u003c\u003c nRead \u003c\u003c \" Bytes, \\\"\" \u003c\u003c strRec \u003c\u003c \"\\\"\" \u003c\u003c endl;\n\n                cout \u003c\u003c strOutput.str();\n            }\n        });\n\n    sock.BindFuncConEstablished([\u0026](TcpSocket* pTcpSocket)\n        {\n            pTcpSocket-\u003eWrite(\"Hallo World\", 11);\n        });\n\n    bool bConnected = sock.Connect(\"127.0.0.1\", 3461); // or \"::1\" for IPv6\n    if (bConnected == false)\n        cout \u003c\u003c \"error creating client socket\" \u003c\u003c endl;\n\n    while (*bStop == false)\n    {\n        this_thread::sleep_for(chrono::milliseconds(10));\n    }\n\n    // The Close call will call the Callback function above\n    // but if we leave the thread, the Instance is destroyed\n    // and we crash. So we disable the Callback by setting a nullptr\n    sock.BindCloseFunction(static_cast\u003cfunction\u003cvoid(BaseSocket*)\u003e\u003e(nullptr)));\n    sock.Close();\n}\n\nint main(int argc, const char* argv[])\n{\n#if defined(_WIN32) || defined(_WIN64)\n    // Detect Memory Leaks\n    _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF | _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG));\n#endif\n\n    bool bStop = false;\n    thread thServer = thread(bind(ServerThread, \u0026bStop));\n    this_thread::sleep_for(chrono::milliseconds(1000));\n    thread thClient = thread(bind(ClientThread, \u0026bStop));\n\n    _getch();\n\n    bStop = true;\n\n    thServer.join();\n    thClient.join();\n\n    return 0;\n}\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftomenz%2Fsocketlib","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftomenz%2Fsocketlib","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftomenz%2Fsocketlib/lists"}