{"id":19244330,"url":"https://github.com/nanoframework/system.net.sockets.udpclient","last_synced_at":"2025-04-21T09:33:31.663Z","repository":{"id":37979201,"uuid":"454058972","full_name":"nanoframework/System.Net.Sockets.UdpClient","owner":"nanoframework","description":"📦 System.Net.Sockets.UdpClient library for .NET nanoFramework","archived":false,"fork":false,"pushed_at":"2025-04-02T12:14:32.000Z","size":132,"stargazers_count":4,"open_issues_count":0,"forks_count":2,"subscribers_count":5,"default_branch":"main","last_synced_at":"2025-04-19T17:14:48.605Z","etag":null,"topics":["csharp","dotnet","embedded-systems","esp32","hacktoberfest","nanoframework","nxp","stm32","texas-instruments","udp"],"latest_commit_sha":null,"homepage":"https://www.nanoframework.net","language":"C#","has_issues":false,"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/nanoframework.png","metadata":{"funding":{"open_collective":"nanoframework","github":"nanoframework"},"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.md","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,"zenodo":null}},"created_at":"2022-01-31T15:25:04.000Z","updated_at":"2025-04-02T12:09:23.000Z","dependencies_parsed_at":"2023-01-31T06:30:31.460Z","dependency_job_id":"5f67b208-324c-4aa0-af72-82d81a2b0041","html_url":"https://github.com/nanoframework/System.Net.Sockets.UdpClient","commit_stats":{"total_commits":113,"total_committers":6,"mean_commits":"18.833333333333332","dds":"0.49557522123893805","last_synced_commit":"fab171714669206d2817dfd249b49ce9a6697e82"},"previous_names":[],"tags_count":61,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nanoframework%2FSystem.Net.Sockets.UdpClient","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nanoframework%2FSystem.Net.Sockets.UdpClient/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nanoframework%2FSystem.Net.Sockets.UdpClient/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nanoframework%2FSystem.Net.Sockets.UdpClient/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nanoframework","download_url":"https://codeload.github.com/nanoframework/System.Net.Sockets.UdpClient/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250032465,"owners_count":21363839,"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":["csharp","dotnet","embedded-systems","esp32","hacktoberfest","nanoframework","nxp","stm32","texas-instruments","udp"],"created_at":"2024-11-09T17:22:56.912Z","updated_at":"2025-04-21T09:33:31.645Z","avatar_url":"https://github.com/nanoframework.png","language":"C#","funding_links":["https://opencollective.com/nanoframework","https://github.com/sponsors/nanoframework"],"categories":[],"sub_categories":[],"readme":"[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=nanoframework_System.Net.Sockets.UdpClient\u0026metric=alert_status)](https://sonarcloud.io/dashboard?id=nanoframework_System.Net.Sockets.UdpClient) [![Reliability Rating](https://sonarcloud.io/api/project_badges/measure?project=nanoframework_System.Net.Sockets.UdpClient\u0026metric=reliability_rating)](https://sonarcloud.io/dashboard?id=nanoframework_System.Net.Sockets.UdpClient) [![License](https://img.shields.io/badge/License-MIT-blue.svg)](LICENSE) [![NuGet](https://img.shields.io/nuget/dt/nanoFramework.System.Net.Sockets.UdpClient.svg?label=NuGet\u0026style=flat\u0026logo=nuget)](https://www.nuget.org/packages/nanoFramework.System.Net.Sockets.UdpClient/) [![#yourfirstpr](https://img.shields.io/badge/first--timers--only-friendly-blue.svg)](https://github.com/nanoframework/Home/blob/main/CONTRIBUTING.md) [![Discord](https://img.shields.io/discord/478725473862549535.svg?logo=discord\u0026logoColor=white\u0026label=Discord\u0026color=7289DA)](https://discord.gg/gCyBu8T)\n\n![nanoFramework logo](https://raw.githubusercontent.com/nanoframework/Home/main/resources/logo/nanoFramework-repo-logo.png)\n\n-----\n\n# System.Net.Sockets.UdpClient\n\nThis API implements the UdpClient class with a pattern similar to the official .NET equivalent [System.Net.Sockets.UdpClient](https://docs.microsoft.com/en-us/dotnet/api/system.net.sockets.udpclient). Beside the lack of the asynchronous methods the receive functions don't use an internal buffer like the .NET API but instead requires a buffer to be passed as part of the call.\n\n***Note:*** *For the `Receive` methods if the buffer is smaller than the packet to receive it will be truncated to the buffer size without warning. Indeed \"lwIP\", the TCP/IP stack used commonly by RTOS, doesn't support the `MSG_TRUNC` socket option in calls to `recvfrom` to return the real length of the datagram when it is longer than the passed buffer opposite to common Un\\*x implementations.*\n\n## Build status\n\n| Component | Build Status | NuGet Package |\n|:-|---|---|\n| nanoFramework.System.Net.Sockets.UdpClient | [![Build Status](https://dev.azure.com/nanoframework/System.Net.Sockets.UdpClient/_apis/build/status/nanoframework.System.Net.Sockets.UdpClient?repoName=nanoframework%2FSystem.Net.Sockets.UdpClient\u0026branchName=main)](https://dev.azure.com/nanoframework/System.Net.Sockets.UdpClient/_build/latest?definitionId=92\u0026repoName=nanoframework%2FSystem.Net.Sockets.UdpClient\u0026branchName=main) | [![NuGet](https://img.shields.io/nuget/v/nanoFramework.System.Net.Sockets.UdpClient.svg?label=NuGet\u0026style=flat\u0026logo=nuget)](https://www.nuget.org/packages/nanoFramework.System.Net.Sockets.UdpClient/) |\n\n## Usage\n\n**Important:** Obviously UdpClient requires a working network connection with a valid IP address. Please check the examples with the Network Helpers on how to connect to a network.\n\nThe UdpClient class provides simple methods for sending and receiving UDP datagrams on an IP network. The current implementation supports only IPv4. IPv6 isn't supported on nanoFramework currently.\n\n### Samples\n\nSamples for `UdpClient` are present in the [nanoFramework Sample repository](https://github.com/nanoframework/Samples).\n\n### Remote host\n\nBecause UDP is a connectionless protocol you don't need to establish a remote host connection before sending and receiving data. But you can define a default remote host that will be used for subsequent `Send` method calls. If you establish a default remote host, you cannot specify a different host when sending datagrams. You can define a default remote host with one of the following methods:\n\n- Create your client using the `UdpClient(string hostname,string remoteport)` constructor.\n- Create an instance and then call the `Connect` method.\n\n### Client usage\n\nUsing `UdpClient` in client mode is pretty easy. You create an UdpClient with one of the constructor, establish or not a default remote host (see above) then you can send and receive message from the network. \n\nThe following code show a typical client server exchange where you first send a message to the server and wait for the server answer:\n\n```C#\n// establish defaut host and port\nUdpClient udpClient = new UdpClient(\"1.2.3.4\", 5000);\nudpClient.Send(Encoding.UTF8.GetBytes(\"Hello server\"));\n\n// receive message\nbyte[] buffer = new byte[1024];\nIPEndPoint ipEndpoint = new IPEndPoint(IPAddress.Any, 0);\nint length = udpClient.Receive(buffer, ref ipEndpoint);\nDebug.WriteLine(Encoding.UTF8.GetString(buffer, 0, length));\n```\n\n### Server usage\n\nWorking as server you bind your `UdpClient` on a local port then you wait for messages from clients. As a server you don't know beforehand the IP address of your clients so you shouldn't define any remote host.\n\nThe following code show a simple echo server:\n\n```C#\n// Run echo protocol on port 5000\nUdpClient udpClient = new UdpClient(5000); \n\n// We limit ourself to a 1024 bytes buffer\nbyte[] buffer = new byte[1024];\nIPEndPoint endpointClient = new IPEndPoint(IPAddress.Any, 0);\n\n// We send back every request we get\nwhile (true)\n{\n    int length = udpClient.Receive(buffer, ref endpointClient);\n    udpClient.Send(buffer,endpointClient);\n}\n```\n\n### Multicast\n\nIf you want to use multicast ensure that you bind your `UdpClient` on the `0.0.0.0` (wilcard) address. If you bind your UdpClient to a specific `IPAddress` you won't receive the multicast datagrams.\n\nBasically for a functional multicast client/server you need to:\n\n- Create your UdpClient without binding it to a specific address.\n- Join a Multicast group by a call to the JoinMulticastGroup method.\n- Receive datagram sent to that group address with call to `Receive`\n- Send message to the group multicast using `Send`\n- Leave the Multicast group by calling the `DropMulticastGroup` method\n\nThe following sample illustrate that basic workflow:\n\n```C#\n// Create your UdpClient without binding it to a specific address\nIPEndPoint iPEndpoint = new IPEndPoint(IPAddress.Any, 5000);\nUdpClient client = new UdpClient(iPEndpoint);\n\n// Join a Multicast group\nIPAddress ipGroupMulticast = IPAddress.Parse(\"239.255.255.250\");\nclient.JoinMulticastGroup(ipGroupMulticast);\n\nbool StopListener = false;\nbyte[] buffer = new byte[2048];\nwhile (!StopListener)\n{\n    IPEndPoint remote = new IPEndPoint(IPAddress.Any, 0);\n    int length = client.Receive(buffer, ref remote);\n    string result = Encoding.UTF8.GetString(buffer, 0, length);\n    if (result == \"Ping\")\n    {\n        buffer = Encoding.UTF8.GetBytes(\"Present\");\n        client.Send(buffer,ipGroupMulticast);\n    }\n    StopListener = (result == \"Exit\");\n}\n\n// Leave the Multicast group\nclient.DropMulticastGroup(ipGroupMulticast);\n```\n\nIf you want to receive your own messages you can enable this by setting the `UdpClient.MulticastLoopback` property to `true`.\n\n## Feedback and documentation\n\nFor documentation, providing feedback, issues and finding out how to contribute please refer to the [Home repo](https://github.com/nanoframework/Home).\n\nJoin our Discord community [here](https://discord.gg/gCyBu8T).\n\n## Credits\n\nThe list of contributors to this project can be found at [CONTRIBUTORS](https://github.com/nanoframework/Home/blob/main/CONTRIBUTORS.md).\n\n## License\n\nThe **nanoFramework** Class Libraries are licensed under the [MIT license](LICENSE.md).\n\n## Code of Conduct\n\nThis project has adopted the code of conduct defined by the Contributor Covenant to clarify expected behaviour in our community.\nFor more information see the [.NET Foundation Code of Conduct](https://dotnetfoundation.org/code-of-conduct).\n\n## .NET Foundation\n\nThis project is supported by the [.NET Foundation](https://dotnetfoundation.org).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnanoframework%2Fsystem.net.sockets.udpclient","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnanoframework%2Fsystem.net.sockets.udpclient","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnanoframework%2Fsystem.net.sockets.udpclient/lists"}