{"id":16002784,"url":"https://github.com/soreing/net-session-cpp","last_synced_at":"2026-05-07T11:32:53.998Z","repository":{"id":110409791,"uuid":"394721738","full_name":"Soreing/net-session-cpp","owner":"Soreing","description":"UDP based newtork session library for C++","archived":false,"fork":false,"pushed_at":"2021-08-12T09:12:25.000Z","size":23,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-05T01:28:37.690Z","etag":null,"topics":["cpp","cross-platform","keepalive","linux","networking","p2p","raw-sockets","threads","udp","windows"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Soreing.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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":"2021-08-10T17:04:26.000Z","updated_at":"2021-08-12T15:39:05.000Z","dependencies_parsed_at":null,"dependency_job_id":"7bbeecb4-7f74-4a03-aeba-ad3fcdf674cd","html_url":"https://github.com/Soreing/net-session-cpp","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/Soreing/net-session-cpp","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Soreing%2Fnet-session-cpp","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Soreing%2Fnet-session-cpp/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Soreing%2Fnet-session-cpp/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Soreing%2Fnet-session-cpp/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Soreing","download_url":"https://codeload.github.com/Soreing/net-session-cpp/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Soreing%2Fnet-session-cpp/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32735149,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-07T02:14:30.463Z","status":"ssl_error","status_checked_at":"2026-05-07T02:14:29.405Z","response_time":62,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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","cross-platform","keepalive","linux","networking","p2p","raw-sockets","threads","udp","windows"],"created_at":"2024-10-08T10:03:54.708Z","updated_at":"2026-05-07T11:32:53.984Z","avatar_url":"https://github.com/Soreing.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# net-session-cpp\n\n# Description\nnet-session-cpp is a Windows/Linux cross platform C++ library that establishes and maintains a network connection between peers using UDP protocol. After the connection has been established, the session sends keepalive packets to maintain the connection and listens to messages that get stored in a message queue.\n\n# Installation\nAdd the folder `net-session` from `/include` in your include path. If you want to compile the library from source, add all `.cpp` files but `source.cpp` from the `/src` folder. Alternatively, you can compile the source code to a static library and include it that way.\n\n# Basic Usage\nSessions are created through an interface class to ensure the management of the resources is not disrupted. You can create an `ISession` object, which you will need to `close()` when you are finished to avoid memory leaks.  \n  \nOnce you have created a session, you can use the `connect()` function with a **port number** and an **IP address** to connect to a remote host. The function returns 0 on success or -1 on error. You can `disconnect()` a socket manually if you want to close the connection without deleting the session \n```c++\nISession ssn;\nif(ssn.connect(59000, \"127.0.0.1\") == 0)\n{   cout \u003c\u003c \"Connected to the remote host\\n\";\n}\nssn.disconnect();\nssn.close()\n```\nWhile the session is connected, you can send or receive data with the `send()` and `recv()` functions. You can send **at most 1024 bytes** of data at once, the rest will get truncated on the peer's side. Both functions return the number of bytes that were sent or received, or -1 on error.\n```c++\nint bytes;\nstring str;\nchar buffer[1024];\n\nwhile (ssn.getState() == Connected)\n{   \n    getline(cin, str);\n    ssn.send(str.c_str(), str.length()+1);\n\n    bytes = ssn.recv(buffer, 1024);\n    if(bytes \u003e 0)\n    {   cout \u003c\u003c buffer \u003c\u003c\"\\n\";\n    }\n}\n```\n# UDP Sockets\nSessions use a cross platform encapsulation of native C sockets implemented with UDP protocol in mind. You can use UDP Sockets to create a server listening for connections relatively easily. You need to have the following:\n- Create a UDP Socket\n- Create an address structure\n- Bind the socket to the address\n- Start receiving messages\n\n```c++\nUDPSocket sock;\nsock.socket(SOCK_DGRAM);\n\nstruct sockaddr_in host, peer;\nhost.sin_family = AF_INET;\nhost.sin_addr.s_addr = INADDR_ANY;\nhost.sin_port = htons(PORT);\n\nsock.bind(\u0026host);\n\nint bytes;\nchar buffer[1024];\nbytes = sock.recvfrom(buffer, 1024, \u0026peer);\n```\nUDPSockets implement the `sendto()` and `recvfrom()` functions, which in addition to sessions' `send()` and `recv()` functions, require an address structure to send to or receive messages from.  \n  \nUDP sockets can be disconnected with the `close()` function.\n\n# Additional Details\n## P2P Connections\nSessions implement another `connect()` function for connecting to a remote peer. Instead of supplying a port and an IP address, it takes an address structure you can receive by a server mediating the connection. The keepalive packets will perform UDP hole punching and keep the reply address open.\n\n## Raw sockets and sessions\nCreating an `ISession` with a bool parameter true will switch the socket from `SOCK_DGRAM` to `SOCK_RAW`. Raw sockets add the IPv4 and UDP headers in the application instead of the Operating System. UDPSockets can also be created with `SOCK_RAW` type instead of `SOCK_DGRAM`.  \n**Raw sockets require admin/root privileges to operate.**\n```c++\n//Session using raw socket\nISession ssn(true);\n\n//UDPSocket using raw Socket\nUDPSocket sock;\nsock.socket(SOCK_DGRAM);\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsoreing%2Fnet-session-cpp","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsoreing%2Fnet-session-cpp","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsoreing%2Fnet-session-cpp/lists"}