nanoFramework.System.Net.Sockets.UdpClient
1.1.65
Prefix Reserved
dotnet add package nanoFramework.System.Net.Sockets.UdpClient --version 1.1.65
NuGet\Install-Package nanoFramework.System.Net.Sockets.UdpClient -Version 1.1.65
<PackageReference Include="nanoFramework.System.Net.Sockets.UdpClient" Version="1.1.65" />
paket add nanoFramework.System.Net.Sockets.UdpClient --version 1.1.65
#r "nuget: nanoFramework.System.Net.Sockets.UdpClient, 1.1.65"
// Install nanoFramework.System.Net.Sockets.UdpClient as a Cake Addin #addin nuget:?package=nanoFramework.System.Net.Sockets.UdpClient&version=1.1.65 // Install nanoFramework.System.Net.Sockets.UdpClient as a Cake Tool #tool nuget:?package=nanoFramework.System.Net.Sockets.UdpClient&version=1.1.65
System.Net.Sockets.UdpClient
This API implements the UdpClient class with a pattern similar to the official .NET equivalent 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.
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.
Build status
Component | Build Status | NuGet Package |
---|---|---|
nanoFramework.System.Net.Sockets.UdpClient |
Usage
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.
The 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.
Samples
Samples for UdpClient
are present in the nanoFramework Sample repository.
Remote host
Because 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:
- Create your client using the
UdpClient(string hostname,string remoteport)
constructor. - Create an instance and then call the
Connect
method.
Client usage
Using 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.
The following code show a typical client server exchange where you first send a message to the server and wait for the server answer:
// establish defaut host and port
UdpClient udpClient = new UdpClient("1.2.3.4", 5000);
udpClient.Send(Encoding.UTF8.GetBytes("Hello server"));
// receive message
byte[] buffer = new byte[1024];
IPEndPoint ipEndpoint = new IPEndPoint(IPAddress.Any, 0);
int length = udpClient.Receive(buffer, ref ipEndpoint);
Debug.WriteLine(Encoding.UTF8.GetString(buffer, 0, length));
Server usage
Working 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.
The following code show a simple echo server:
// Run echo protocol on port 5000
UdpClient udpClient = new UdpClient(5000);
// We limit ourself to a 1024 bytes buffer
byte[] buffer = new byte[1024];
IPEndPoint endpointClient = new IPEndPoint(IPAddress.Any, 0);
// We send back every request we get
while (true)
{
int length = udpClient.Receive(buffer, ref endpointClient);
udpClient.Send(buffer,endpointClient);
}
Multicast
If 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.
Basically for a functional multicast client/server you need to:
- Create your UdpClient without binding it to a specific address.
- Join a Multicast group by a call to the JoinMulticastGroup method.
- Receive datagram sent to that group address with call to
Receive
- Send message to the group multicast using
Send
- Leave the Multicast group by calling the
DropMulticastGroup
method
The following sample illustrate that basic workflow:
// Create your UdpClient without binding it to a specific address
IPEndPoint iPEndpoint = new IPEndPoint(IPAddress.Any, 5000);
UdpClient client = new UdpClient(iPEndpoint);
// Join a Multicast group
IPAddress ipGroupMulticast = IPAddress.Parse("239.255.255.250");
client.JoinMulticastGroup(ipGroupMulticast);
bool StopListener = false;
byte[] buffer = new byte[2048];
while (!StopListener)
{
IPEndPoint remote = new IPEndPoint(IPAddress.Any, 0);
int length = client.Receive(buffer, ref remote);
string result = Encoding.UTF8.GetString(buffer, 0, length);
if (result == "Ping")
{
buffer = Encoding.UTF8.GetBytes("Present");
client.Send(buffer,ipGroupMulticast);
}
StopListener = (result == "Exit");
}
// Leave the Multicast group
client.DropMulticastGroup(ipGroupMulticast);
If you want to receive your own messages you can enable this by setting the UdpClient.MulticastLoopback
property to true
.
Feedback and documentation
For documentation, providing feedback, issues and finding out how to contribute please refer to the Home repo.
Join our Discord community here.
Credits
The list of contributors to this project can be found at CONTRIBUTORS.
License
The nanoFramework Class Libraries are licensed under the MIT license.
Code of Conduct
This project has adopted the code of conduct defined by the Contributor Covenant to clarify expected behaviour in our community. For more information see the .NET Foundation Code of Conduct.
.NET Foundation
This project is supported by the .NET Foundation.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET Framework | net is compatible. |
-
- nanoFramework.CoreLibrary (>= 1.15.5)
- nanoFramework.System.Net (>= 1.11.1)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on nanoFramework.System.Net.Sockets.UdpClient:
Package | Downloads |
---|---|
nanoFramework.Iot.Device.MulticastDns
This package includes the .NET IoT Core binding Iot.Device.MulticastDns for .NET nanoFramework C# projects. |
GitHub repositories (1)
Showing the top 1 popular GitHub repositories that depend on nanoFramework.System.Net.Sockets.UdpClient:
Repository | Stars |
---|---|
nanoframework/Samples
🍬 Code samples from the nanoFramework team used in testing, proof of concepts and other explorational endeavours
|
Version | Downloads | Last updated |
---|---|---|
1.1.65 | 357 | 7/29/2024 |
1.1.63 | 779 | 6/4/2024 |
1.1.61 | 2,353 | 1/29/2024 |
1.1.59 | 232 | 1/26/2024 |
1.1.55 | 473 | 11/10/2023 |
1.1.51 | 326 | 11/9/2023 |
1.1.48 | 1,970 | 5/16/2023 |
1.1.43 | 722 | 12/28/2022 |
1.1.41 | 600 | 12/28/2022 |
1.1.39 | 635 | 12/27/2022 |
1.1.37 | 672 | 12/23/2022 |
1.1.34 | 896 | 10/26/2022 |
1.1.32 | 720 | 10/26/2022 |
1.1.30 | 716 | 10/26/2022 |
1.1.28 | 759 | 10/25/2022 |
1.1.25 | 711 | 10/24/2022 |
1.1.23 | 740 | 10/23/2022 |
1.1.21 | 775 | 10/10/2022 |
1.1.19 | 730 | 10/8/2022 |
1.1.16 | 731 | 9/22/2022 |
1.1.14 | 723 | 9/22/2022 |
1.1.12 | 740 | 9/15/2022 |
1.1.10 | 759 | 8/5/2022 |
1.1.8 | 709 | 8/4/2022 |
1.1.6 | 721 | 8/4/2022 |
1.1.4 | 744 | 8/3/2022 |
1.1.2 | 703 | 8/3/2022 |
1.0.0.18 | 715 | 8/3/2022 |
1.0.0.16 | 808 | 6/13/2022 |
1.0.0.14 | 799 | 6/8/2022 |
1.0.0.12 | 760 | 5/26/2022 |
1.0.0.10 | 780 | 5/18/2022 |
1.0.0.8 | 746 | 5/3/2022 |
1.0.0 | 799 | 3/29/2022 |
1.0.0-preview.23 | 132 | 3/29/2022 |
1.0.0-preview.21 | 134 | 3/17/2022 |
1.0.0-preview.19 | 115 | 3/14/2022 |
1.0.0-preview.17 | 128 | 3/14/2022 |
1.0.0-preview.15 | 125 | 3/14/2022 |
1.0.0-preview.13 | 127 | 2/18/2022 |
1.0.0-preview.6 | 174 | 2/9/2022 |