Skip to content

Commit 204517f

Browse files
authored
Update Readme and add ref to samples (#2)
***NO_CI***
1 parent d2caf39 commit 204517f

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed

README.md

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,98 @@ This API implements the UdpClient class with a pattern similar to the official .
1717
| 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&branchName=main)](https://dev.azure.com/nanoframework/System.Net.Sockets.UdpClient/_build/latest?definitionId=92&repoName=nanoframework%2FSystem.Net.Sockets.UdpClient&branchName=main) | [![NuGet](https://img.shields.io/nuget/v/nanoFramework.System.Net.Sockets.UdpClient.svg?label=NuGet&style=flat&logo=nuget)](https://www.nuget.org/packages/nanoFramework.System.Net.Sockets.UdpClient/) |
1818
| nanoFramework.System.Net.Sockets.UdpClient (preview) | [![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&branchName=develop)](https://dev.azure.com/nanoframework/System.Net.Sockets.UdpClient/_build/latest?definitionId=92&repoName=nanoframework%2FSystem.Net.Sockets.UdpClient&branchName=develop) | [![NuGet](https://img.shields.io/nuget/vpre/nanoFramework.System.Net.Sockets.UdpClient.svg?label=NuGet&style=flat&logo=nuget)](https://www.nuget.org/packages/nanoFramework.System.Net.Sockets.UdpClient/) |
1919

20+
## Usage
21+
22+
**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.
23+
24+
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.
25+
26+
### Samples
27+
Samples for `UdpClient` are present in the [nanoFramework Sample repository](https://github.com/nanoframework/Samples).
28+
29+
### Remote host
30+
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:
31+
- Create your client using the `UdpClient(string hostname,string remoteport)` constructor.
32+
- Create an instance and then call the `Connect` method.
33+
34+
### Client usage
35+
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.
36+
37+
The following code show a typical client server exchange where you first send a message to the server and wait for the server answer:
38+
39+
```C#
40+
// establish defaut host and port
41+
UdpClient udpClient = new UdpClient("1.2.3.4", 5000);
42+
udpClient.Send(Encoding.UTF8.GetBytes("Hello server"));
43+
44+
// receive message
45+
byte[] buffer = new byte[1024];
46+
IPEndPoint ipEndpoint = new IPEndPoint(IPAddress.Any, 0);
47+
int length = udpClient.Receive(buffer, ref ipEndpoint);
48+
Debug.WriteLine(Encoding.UTF8.GetString(buffer, 0, length));
49+
```
50+
51+
### Server usage
52+
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.
53+
54+
The following code show a simple echo server:
55+
```c#
56+
// Run echo protocol on port 5000
57+
UdpClient udpClient = new UdpClient(5000);
58+
59+
// We limit ourself to a 1024 bytes buffer
60+
byte[] buffer = new byte[1024];
61+
IPEndPoint endpointClient = new IPEndPoint(IPAddress.Any, 0);
62+
63+
// We send back every request we get
64+
while (true)
65+
{
66+
int length = udpClient.Receive(buffer, ref endpointClient);
67+
udpClient.Send(buffer,endpointClient);
68+
}
69+
```
70+
71+
### Multicast
72+
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.
73+
74+
Basically for a functional multicast client/server you need to:
75+
- Create your UdpClient without binding it to a specific address.
76+
- Join a Multicast group by a call to the JoinMulticastGroup method.
77+
- Receive datagram sent to that group address with call to `Receive`
78+
- Send message to the group multicast using `Send`
79+
- Leave the Multicast group by calling the `DropMulticastGroup` method
80+
81+
The following sample illustrate that basic workflow:
82+
83+
```C#
84+
// Create your UdpClient without binding it to a specific address
85+
IPEndPoint iPEndpoint = new IPEndPoint(IPAddress.Any, 5000);
86+
UdpClient client = new UdpClient(iPEndpoint);
87+
88+
// Join a Multicast group
89+
IPAddress ipGroupMulticast = IPAddress.Parse("239.255.255.250");
90+
client.JoinMulticastGroup(ipGroupMulticast);
91+
92+
bool StopListener = false;
93+
byte[] buffer = new byte[2048];
94+
while (!StopListener)
95+
{
96+
IPEndPoint remote = new IPEndPoint(IPAddress.Any, 0);
97+
int length = client.Receive(buffer, ref remote);
98+
string result = Encoding.UTF8.GetString(buffer, 0, length);
99+
if (result == "Ping")
100+
{
101+
buffer = Encoding.UTF8.GetBytes("Present");
102+
client.Send(buffer,ipGroupMulticast);
103+
}
104+
StopListener = (result == "Exit");
105+
}
106+
107+
// Leave the Multicast group
108+
client.DropMulticastGroup(ipGroupMulticast);
109+
```
110+
If you want to receive your own messages you can enable this by setting the `UdpClient.MulticastLoopback` property to `true`.
111+
20112
## Feedback and documentation
21113

22114
For documentation, providing feedback, issues and finding out how to contribute please refer to the [Home repo](https://github.com/nanoframework/Home).

0 commit comments

Comments
 (0)