Skip to content

Commit ff4e967

Browse files
authored
UdpClient sample pack (#197)
1 parent 1de4834 commit ff4e967

File tree

15 files changed

+660
-0
lines changed

15 files changed

+660
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="Current" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup Label="Globals">
4+
<NanoFrameworkProjectSystemPath>$(MSBuildExtensionsPath)\nanoFramework\v1.0\</NanoFrameworkProjectSystemPath>
5+
</PropertyGroup>
6+
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.Default.props" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.Default.props')" />
7+
<PropertyGroup>
8+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
9+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
10+
<ProjectTypeGuids>{11A8DD76-328B-46DF-9F39-F559912D0360};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
11+
<ProjectGuid>eda31032-9d78-4f4f-89e1-1cd246131ed3</ProjectGuid>
12+
<OutputType>Exe</OutputType>
13+
<AppDesignerFolder>Properties</AppDesignerFolder>
14+
<FileAlignment>512</FileAlignment>
15+
<RootNamespace>DumpSSDPRequests</RootNamespace>
16+
<AssemblyName>DumpSSDPRequests</AssemblyName>
17+
<TargetFrameworkVersion>v1.0</TargetFrameworkVersion>
18+
</PropertyGroup>
19+
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.props" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.props')" />
20+
<ItemGroup>
21+
<Compile Include="Program.cs" />
22+
<Compile Include="Properties\AssemblyInfo.cs" />
23+
</ItemGroup>
24+
<ItemGroup>
25+
<Reference Include="mscorlib">
26+
<HintPath>..\packages\nanoFramework.CoreLibrary.1.12.0-preview.5\lib\mscorlib.dll</HintPath>
27+
</Reference>
28+
<Reference Include="nanoFramework.Runtime.Events">
29+
<HintPath>..\packages\nanoFramework.Runtime.Events.1.10.0-preview.6\lib\nanoFramework.Runtime.Events.dll</HintPath>
30+
</Reference>
31+
<Reference Include="nanoFramework.System.Text">
32+
<HintPath>..\packages\nanoFramework.System.Text.1.1.3-preview.13\lib\nanoFramework.System.Text.dll</HintPath>
33+
</Reference>
34+
<Reference Include="System.Net">
35+
<HintPath>..\packages\nanoFramework.System.Net.1.8.0-preview.26\lib\System.Net.dll</HintPath>
36+
</Reference>
37+
<Reference Include="System.Net.Sockets.UdpClient">
38+
<HintPath>..\packages\nanoframework.System.Net.Sockets.UdpClient.1.0.0-preview.6\lib\System.Net.Sockets.UdpClient.dll</HintPath>
39+
</Reference>
40+
<Reference Include="System.Threading">
41+
<HintPath>..\packages\nanoFramework.System.Threading.1.0.4-preview.14\lib\System.Threading.dll</HintPath>
42+
</Reference>
43+
</ItemGroup>
44+
<ItemGroup>
45+
<None Include="packages.config" />
46+
</ItemGroup>
47+
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets')" />
48+
<ProjectExtensions>
49+
<ProjectCapabilities>
50+
<ProjectConfigurationsDeclaredAsItems />
51+
</ProjectCapabilities>
52+
</ProjectExtensions>
53+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
//
2+
// Copyright (c) .NET Foundation and Contributors
3+
// See LICENSE file in the project root for full license information.
4+
//
5+
6+
//#define HAS_WIFI
7+
8+
using System;
9+
using System.Diagnostics;
10+
using System.Threading;
11+
using System.Text;
12+
using System.Net;
13+
using System.Net.Sockets;
14+
using nanoFramework.Networking;
15+
16+
namespace DumpSSDPRequests
17+
{
18+
public class Program
19+
{
20+
public static void Main()
21+
{
22+
#if HAS_WIFI
23+
string MySsid = "ssid";
24+
string MyPassword = "password";
25+
#endif
26+
Debug.WriteLine("Hello from nanoFramework!");
27+
28+
CancellationTokenSource cs = new(10 * 1000); //10 seconds
29+
bool success;
30+
31+
#if HAS_WIFI
32+
success = WiFiNetworkHelper.ConnectDhcp(MySsid, MyPassword, requiresDateTime: true, token: cs.Token);
33+
#else
34+
success = NetworkHelper.SetupAndConnectNetwork(cs.Token, true);
35+
#endif
36+
if (!success)
37+
{
38+
Debug.WriteLine($"Can't get a proper IP address and DateTime, error: {NetworkHelper.Status}.");
39+
if (NetworkHelper.HelperException != null)
40+
{
41+
Debug.WriteLine($"ex: {NetworkHelper.HelperException}");
42+
}
43+
44+
return;
45+
}
46+
else
47+
{
48+
Debug.WriteLine($"{DateTime.UtcNow} Network connected");
49+
}
50+
51+
52+
Debug.WriteLine("Starting SSD monitor");
53+
byte[] buffer = new byte[2048];
54+
IPAddress ipSSD = IPAddress.Parse("239.255.255.250");
55+
IPEndPoint iPEndpoint = new IPEndPoint(IPAddress.Any, 1900);
56+
57+
UdpClient client = new UdpClient(iPEndpoint);
58+
client.JoinMulticastGroup(ipSSD);
59+
try
60+
{
61+
while (true)
62+
{
63+
IPEndPoint remote = new IPEndPoint(IPAddress.Any, 0);
64+
int length = client.Receive(buffer, ref remote);
65+
string result = Encoding.UTF8.GetString(buffer, 0, length);
66+
Debug.WriteLine($"{DateTime.UtcNow} <- {remote}");
67+
Debug.WriteLine(result);
68+
}
69+
}
70+
finally
71+
{
72+
client.DropMulticastGroup(ipSSD);
73+
Thread.Sleep(Timeout.Infinite);
74+
}
75+
76+
Thread.Sleep(Timeout.Infinite);
77+
78+
}
79+
}
80+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("CSharp.BlankApplication")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("CSharp.BlankApplication")]
13+
[assembly: AssemblyCopyright("Copyright © ")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// Version information for an assembly consists of the following four values:
23+
//
24+
// Major Version
25+
// Minor Version
26+
// Build Number
27+
// Revision
28+
//
29+
// You can specify all the values or you can default the Build and Revision Numbers
30+
// by using the '*' as shown below:
31+
// [assembly: AssemblyVersion("1.0.*")]
32+
[assembly: AssemblyVersion("1.0.0.0")]
33+
[assembly: AssemblyFileVersion("1.0.0.0")]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<packages>
3+
<package id="nanoFramework.CoreLibrary" version="1.12.0-preview.5" targetFramework="netnanoframework10" />
4+
<package id="nanoFramework.Runtime.Events" version="1.10.0-preview.6" targetFramework="netnanoframework10" />
5+
<package id="nanoFramework.System.Device.WiFi" version="1.4.0-preview.34" targetFramework="netnanoframework10" />
6+
<package id="nanoFramework.System.Net" version="1.8.0-preview.26" targetFramework="netnanoframework10" />
7+
<package id="nanoframework.System.Net.Sockets.UdpClient" version="1.0.0-preview.6" targetFramework="netnanoframework10" />
8+
<package id="nanoFramework.System.Text" version="1.1.3-preview.13" targetFramework="netnanoframework10" />
9+
<package id="nanoFramework.System.Threading" version="1.0.4-preview.14" targetFramework="netnanoframework10" />
10+
</packages>
+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
//
2+
// Copyright (c) .NET Foundation and Contributors
3+
// See LICENSE file in the project root for full license information.
4+
//
5+
6+
//#define HAS_WIFI
7+
8+
using System;
9+
using System.Diagnostics;
10+
using System.Threading;
11+
using System.Text;
12+
using System.Net;
13+
using System.Net.Sockets;
14+
using nanoFramework.Networking;
15+
16+
namespace QOTDClient
17+
{
18+
public class Program
19+
{
20+
public static void Main()
21+
{
22+
#if HAS_WIFI
23+
string MySsid = "ssid";
24+
string MyPassword = "password";
25+
#endif
26+
Debug.WriteLine("Hello from nanoFramework!");
27+
28+
CancellationTokenSource cs = new(10 * 1000); //10 seconds
29+
bool success;
30+
31+
#if HAS_WIFI
32+
success = WiFiNetworkHelper.ConnectDhcp(MySsid, MyPassword, requiresDateTime: true, token: cs.Token);
33+
#else
34+
success = NetworkHelper.SetupAndConnectNetwork(cs.Token, true);
35+
#endif
36+
if (!success)
37+
{
38+
Debug.WriteLine($"Can't get a proper IP address and DateTime, error: {NetworkHelper.Status}.");
39+
if (NetworkHelper.HelperException != null)
40+
{
41+
Debug.WriteLine($"ex: {NetworkHelper.HelperException}");
42+
}
43+
44+
return;
45+
}
46+
else
47+
{
48+
Debug.WriteLine($"{DateTime.UtcNow} Network connected");
49+
}
50+
51+
UdpClient udpClient = new UdpClient("djxmmx.net", 17); // Quote of the day public server
52+
udpClient.Client.ReceiveTimeout = 5000; // 5 sec time out
53+
54+
byte[] buffer = new byte[1024];
55+
IPEndPoint ipEndpoint = new IPEndPoint(IPAddress.Any, 0);
56+
57+
while (true)
58+
{
59+
Debug.WriteLine("Getting quote of the day");
60+
udpClient.Send(Encoding.UTF8.GetBytes(" "));
61+
try
62+
{
63+
int length = udpClient.Receive(buffer, ref ipEndpoint);
64+
Debug.WriteLine(Encoding.UTF8.GetString(buffer, 0, length));
65+
Thread.Sleep(5000);
66+
}
67+
catch (SocketException ex) when (ex.ErrorCode == (int)SocketError.TimedOut)
68+
{
69+
Debug.WriteLine("Time out!");
70+
}
71+
}
72+
}
73+
}
74+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("CSharp.BlankApplication")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("CSharp.BlankApplication")]
13+
[assembly: AssemblyCopyright("Copyright © ")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// Version information for an assembly consists of the following four values:
23+
//
24+
// Major Version
25+
// Minor Version
26+
// Build Number
27+
// Revision
28+
//
29+
// You can specify all the values or you can default the Build and Revision Numbers
30+
// by using the '*' as shown below:
31+
// [assembly: AssemblyVersion("1.0.*")]
32+
[assembly: AssemblyVersion("1.0.0.0")]
33+
[assembly: AssemblyFileVersion("1.0.0.0")]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="Current" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup Label="Globals">
4+
<NanoFrameworkProjectSystemPath>$(MSBuildExtensionsPath)\nanoFramework\v1.0\</NanoFrameworkProjectSystemPath>
5+
</PropertyGroup>
6+
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.Default.props" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.Default.props')" />
7+
<PropertyGroup>
8+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
9+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
10+
<ProjectTypeGuids>{11A8DD76-328B-46DF-9F39-F559912D0360};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
11+
<ProjectGuid>4a6082d2-8f0b-499a-9b5d-9de9ac5c6b51</ProjectGuid>
12+
<OutputType>Exe</OutputType>
13+
<AppDesignerFolder>Properties</AppDesignerFolder>
14+
<FileAlignment>512</FileAlignment>
15+
<RootNamespace>QOTDClient</RootNamespace>
16+
<AssemblyName>QOTDClient</AssemblyName>
17+
<TargetFrameworkVersion>v1.0</TargetFrameworkVersion>
18+
</PropertyGroup>
19+
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.props" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.props')" />
20+
<ItemGroup>
21+
<Compile Include="Program.cs" />
22+
<Compile Include="Properties\AssemblyInfo.cs" />
23+
</ItemGroup>
24+
<ItemGroup>
25+
<Reference Include="mscorlib">
26+
<HintPath>..\packages\nanoFramework.CoreLibrary.1.12.0-preview.5\lib\mscorlib.dll</HintPath>
27+
</Reference>
28+
<Reference Include="nanoFramework.Runtime.Events">
29+
<HintPath>..\packages\nanoFramework.Runtime.Events.1.10.0-preview.6\lib\nanoFramework.Runtime.Events.dll</HintPath>
30+
</Reference>
31+
<Reference Include="nanoFramework.System.Text">
32+
<HintPath>..\packages\nanoFramework.System.Text.1.1.3-preview.13\lib\nanoFramework.System.Text.dll</HintPath>
33+
</Reference>
34+
<Reference Include="System.Net">
35+
<HintPath>..\packages\nanoFramework.System.Net.1.8.0-preview.26\lib\System.Net.dll</HintPath>
36+
</Reference>
37+
<Reference Include="System.Net.Sockets.UdpClient">
38+
<HintPath>..\packages\nanoframework.System.Net.Sockets.UdpClient.1.0.0-preview.6\lib\System.Net.Sockets.UdpClient.dll</HintPath>
39+
</Reference>
40+
<Reference Include="System.Threading">
41+
<HintPath>..\packages\nanoFramework.System.Threading.1.0.4-preview.14\lib\System.Threading.dll</HintPath>
42+
</Reference>
43+
</ItemGroup>
44+
<ItemGroup>
45+
<None Include="packages.config" />
46+
</ItemGroup>
47+
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets')" />
48+
<ProjectExtensions>
49+
<ProjectCapabilities>
50+
<ProjectConfigurationsDeclaredAsItems />
51+
</ProjectCapabilities>
52+
</ProjectExtensions>
53+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<packages>
3+
<package id="nanoFramework.CoreLibrary" version="1.12.0-preview.5" targetFramework="netnanoframework10" />
4+
<package id="nanoFramework.Runtime.Events" version="1.10.0-preview.6" targetFramework="netnanoframework10" />
5+
<package id="nanoFramework.System.Device.WiFi" version="1.4.0-preview.34" targetFramework="netnanoframework10" />
6+
<package id="nanoFramework.System.Net" version="1.8.0-preview.26" targetFramework="netnanoframework10" />
7+
<package id="nanoframework.System.Net.Sockets.UdpClient" version="1.0.0-preview.6" targetFramework="netnanoframework10" />
8+
<package id="nanoFramework.System.Text" version="1.1.3-preview.13" targetFramework="netnanoframework10" />
9+
<package id="nanoFramework.System.Threading" version="1.0.4-preview.14" targetFramework="netnanoframework10" />
10+
</packages>

samples/UdpClient/README.md

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# UdpClient sample pack
2+
3+
Shows how to use UdpClient API in various situations.
4+
5+
## Hardware requirements
6+
7+
An hardware device running a nanoFramework image with networking capabilities enabled.
8+
9+
## Samples provided
10+
11+
Three samples are provided:
12+
- **QOTDClient** : show how to use `UdpClient` as an udp client. It connect to a public `Quote of the day` server and display the result.
13+
14+
- **UdpEchoServer** : show how to use `UdpClient` as an udp server. It implements a simple Udp server that listen to messages and echo them back to the sender. You need a companion sender application to test it. The `Sender.ipynb` notebook in the folder can be used with .net interactive in vscode to send messages to the server (check https://marketplace.visualstudio.com/items?itemName=ms-dotnettools.dotnet-interactive-vscode on how to proceed).
15+
16+
- **DumpSSDPRequests** : show how to join a multicast group and get multicast messages. This sample display the SSDP messages that are sent by the different devices on the local network.
17+
18+
## Build the sample
19+
20+
1. Start Microsoft Visual Studio 2019/2022 and select `File > Open > Project/Solution`.
21+
1. Starting in the folder where you unzipped the samples/cloned the repository, go to the subfolder for this specific sample. Double-click the Visual Studio Solution (.sln) file.
22+
1. Press `Ctrl+Shift+B`, or select `Build > Build Solution`.
23+
24+
## Run the sample
25+
26+
You need to select the sample you want to run. If you want to use wifi, you must include a reference to the `System.Device.WiFi` package, define the HAS_WIFI variable and set your wifi parameters in the code.
27+
28+
The next steps depend on whether you just want to deploy the sample or you want to both deploy and run it.
29+
30+
### Deploying the sample
31+
32+
- Select `Build > Deploy Solution`.
33+
34+
### Deploying and running the sample
35+
36+
- To debug the sample and then run it, press F5 or select `Debug > Start Debugging`.
37+
38+
> **Important**: Before deploying or running the sample, please make sure your device is visible in the Device Explorer.
39+
40+
> **Tip**: To display the Device Explorer, go to Visual Studio menus: `View > Other Windows > Device Explorer`.

0 commit comments

Comments
 (0)