{"id":29014064,"url":"https://github.com/armmbed/sockets","last_synced_at":"2025-06-25T20:12:43.323Z","repository":{"id":25493043,"uuid":"28924121","full_name":"ARMmbed/sockets","owner":"ARMmbed","description":"mbed sockets library abstraction layer","archived":false,"fork":false,"pushed_at":"2016-12-19T09:18:44.000Z","size":295,"stargazers_count":6,"open_issues_count":16,"forks_count":18,"subscribers_count":48,"default_branch":"master","last_synced_at":"2023-08-03T07:54:44.419Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ARMmbed.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-01-07T16:57:58.000Z","updated_at":"2022-01-17T17:05:48.000Z","dependencies_parsed_at":"2022-08-24T00:00:42.230Z","dependency_job_id":null,"html_url":"https://github.com/ARMmbed/sockets","commit_stats":null,"previous_names":[],"tags_count":38,"template":null,"template_full_name":null,"purl":"pkg:github/ARMmbed/sockets","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ARMmbed%2Fsockets","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ARMmbed%2Fsockets/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ARMmbed%2Fsockets/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ARMmbed%2Fsockets/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ARMmbed","download_url":"https://codeload.github.com/ARMmbed/sockets/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ARMmbed%2Fsockets/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":261945409,"owners_count":23234244,"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":"2025-06-25T20:12:42.493Z","updated_at":"2025-06-25T20:12:43.312Z","avatar_url":"https://github.com/ARMmbed.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# mbed C++ Socket API\nThe mbed C++ Socket API provides an interface to sockets which will be familiar to users of BSD sockets, but there are several differences compared to the typical BSD socket implementations.  In typical desktop BSD socket applications, the program will block a thread, while waiting for each socket call to complete.  This is conceptually simple, but not well-suited to event-based embedded systems.  In the C++ Socket API, each socket has a set of callbacks which trigger when events complete, much like the BSD Sockets work in asynchronous mode; instead of having a single SIGIO handler however, the C++ Socket API provides an event handler for each type of event.\n\n## Usage Notes\nThis version is an alpha and there are some known problems.\n### Event handler context\nEvent handling currently occurs in the interrupt context.  In an upcoming release, there will be provision for event handling in the main loop.\n### Error handling\nErrors are forwarded to the event registered with setOnError().  If this event is not set, errors are ignored.  All applications should register an onError event handler.\n\n## Getting Started\nThe best place to start is the mbed-example-network module.  There are simple examples there.  However, for a simple overview, there are several examples below.\n\n### UDP Examples\nCreating a UDP Socket\n```C++\nUDPSocket s(SOCKET_STACK_LWIP_IPV4);\n```\nSet an error handler\n```C++\ns.setOnError(onError);\n```\nOpening a UDP Socket\n```C++\ns.open(SOCKET_AF_INET4);\n```\nSend some data\n```C++\ns.send_to(data, dlen, addr, port);\n```\nRecieve some data\n```C++\ns.setOnReadable(onRecv);\n```\n\n### TCP Examples\nCreating a TCP Socket\n```C++\nTCPStream s(SOCKET_STACK_LWIP_IPV4);\n```\nOpening a TCP Socket\n```C++\ns.open(SOCKET_AF_INET4);\n```\nConnect to a host\n```C++\ns.connect(addr, port, onConnect);\n```\nSet the disconnect handler\n```C++\ns.setOnDisconnect(onDisconnect);\n```\nSend some data\n```C++\ns.send(data, dlen);\n```\nReceive some data\n```C++\ns.setOnReadable(onRecv);\n```\nClose the socket\n```C++\ns.close();\n```\n\n### DNS Example\nThis is a complete example of resolving an address with DNS\n```C++\n#include \"sockets/v0/UDPSocket.h\"\n#include \"sal-stack-lwip/lwipv4_init.h\"\n#include \"sal-iface-eth/EthernetInterface.h\"\nusing namespace mbed::Sockets::v0;\nclass Resolver {\nprivate:\n    UDPSocket _sock;\npublic:\n    Resolver() : _sock(SOCKET_STACK_LWIP_IPV4) {\n        _sock.open(SOCKET_AF_INET4);\n    }\n    void onDNS(Socket *s, struct socket_addr addr, const char *domain) {\n        (void) s;\n        SocketAddr sa;\n        char buf[16];\n        sa.setAddr(\u0026addr);\n        sa.fmtIPv4(buf,sizeof(buf));\n        printf(\"Resolved %s to %s\\r\\n\", domain, buf);\n    }\n    socket_error_t resolve(const char * address) {\n        printf(\"Resolving %s...\\r\\n\", address);\n        return _sock.resolve(address,UDPSocket::DNSHandler_t(this, \u0026Resolver::onDNS));\n    }\n};\n\nEthernetInterface eth;\nResolver *r;\nvoid app_start(int argc, char *argv[]) {\n    (void) argc;\n    (void) argv;\n    static Serial pc(USBTX, USBRX);\n    pc.baud(115200);\n    printf(\"Connecting to network...\\r\\n\");\n    eth.init();\n    eth.connect();\n    printf(\"Connected\\r\\n\");\n    lwipv4_socket_init();\n    r = new Resolver();\n    r-\u003eresolve(\"mbed.org\");\n}\n\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Farmmbed%2Fsockets","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Farmmbed%2Fsockets","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Farmmbed%2Fsockets/lists"}