Skip to content

Commit e26cbd9

Browse files
authored
Updates for TcpClient repo (#2)
* Updates for TcpClient project * Remove TcpTest Ops, this was meant to be removed and used a as basis for sample * Updated as per review * Fix connect issue * Update readme & check code formatting * Changes to keep SonarCloud happy * Update README.md * Update project to latest to include new streams assembly * Update nanoframework.System.Net.Sockets.TcpClient.nuspec
1 parent c36b03b commit e26cbd9

21 files changed

+1080
-261
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Copyright (c) .NET Foundation and Contributors
2+
# See LICENSE file in the project root for full license information.
3+
4+
# This workflow will periodically check .NET nanoFramework dependencies and updates them in the repository it's running.
5+
6+
name: Daily update dependencies
7+
8+
on:
9+
schedule:
10+
# At 00:00 UTC every day.
11+
- cron: '00 00 * * *'
12+
repository_dispatch:
13+
types: update-dependencies
14+
15+
defaults:
16+
run:
17+
shell: pwsh
18+
19+
jobs:
20+
update-dotnet-preview:
21+
name: Update .NET nanoFramework dependencies
22+
timeout-minutes: 15
23+
runs-on: windows-latest
24+
env:
25+
GITHUB_TOKEN: ${{ github.token }}
26+
steps:
27+
- name: Checkout
28+
uses: actions/checkout@v2
29+
- name: Update dependencies
30+
uses: nanoframework/nanodu@v1
31+
with:
32+
solutionsToCheck: 'nanoframework.System.Net.Sockets.TcpClient.sln'

.github_changelog_generator

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
user=nanoframework
2+
project=System.Net.Sockets.TcpClient
3+
issues=false
4+
add_issues_wo_labels=false
5+
add_pr_wo_labels=false
6+
add_issues_wo_labels=false
7+
filter_issues_by_milestone=false
8+
exclude_labels=Area: Config-and-Build,Area: Infrastructure-and-Organization,reverted
9+
enhancement_labels=Type: enhancement
10+
bug_labels=Type: bug
11+
merge_prefix=**Documentation and other chores:**
12+
unreleased_label=**Changes available only in 'Preview' NuGet packages:**
13+
author=false

README.md

Lines changed: 203 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,203 @@
1-
# System.Net.Sockets.TcpClient
1+
2+
[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=nanoframework_System.Net.Sockets.TcpClient&metric=alert_status)](https://sonarcloud.io/dashboard?id=nanoframework_System.Net.Sockets.TcpClient) [![Reliability Rating](https://sonarcloud.io/api/project_badges/measure?project=nanoframework_System.Net.Sockets.TcpClient&metric=reliability_rating)](https://sonarcloud.io/dashboard?id=nanoframework_System.Net.Sockets.TcpClient) [![License](https://img.shields.io/badge/License-MIT-blue.svg)](LICENSE) [![NuGet](https://img.shields.io/nuget/dt/nanoFramework.System.Net.Sockets.TcpClient.svg?label=NuGet&style=flat&logo=nuget)](https://www.nuget.org/packages/nanoFramework.System.Net.Sockets.TcpClient/) [![#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&logoColor=white&label=Discord&color=7289DA)](https://discord.gg/gCyBu8T)
3+
4+
![nanoFramework logo](https://raw.githubusercontent.com/nanoframework/Home/main/resources/logo/nanoFramework-repo-logo.png)
5+
6+
-----
7+
8+
# System.Net.Sockets.TcpClient
9+
10+
This API implements the TcpListener and TcpClient classes with a pattern similar to the official .NET equivalent. [System.NET.Sockets.TcpClient](https://docs.microsoft.com/en-us/dotnet/api/system.net.sockets.TcpClient).
11+
12+
These are wrapper classes for the Socket when using TCP connections.
13+
The nanoframework implementation of TcpClient doesn't include the asynchronous methods and the Connected property.
14+
15+
16+
## Build status
17+
18+
| Component | Build Status | NuGet Package |
19+
|:-|---|---|
20+
| nanoFramework.System.Net.Sockets.TcpClient | [![Build Status](https://dev.azure.com/nanoframework/System.Net.Sockets.TcpClient/_apis/build/status/System.Net.Sockets.TcpClient?branchName=main)](https://dev.azure.com/nanoframework/System.Net.Sockets.TcpClient/_build/latest?definitionId=91&branchName=main) | [![NuGet](https://img.shields.io/nuget/v/nanoFramework.System.Net.Sockets.TcpClient.svg?label=NuGet&style=flat&logo=nuget)](https://www.nuget.org/packages/nanoFramework.System.Net.Sockets.TcpClient/) |
21+
| nanoFramework.System.Net.Sockets.TcpClient (preview) | [![Build Status](https://dev.azure.com/nanoframework/System.Net.Sockets.TcpClient/_apis/build/status/System.Net.Sockets.TcpClient?branchName=develop)](https://dev.azure.com/nanoframework/System.Net.Sockets.TcpClient/_build/latest?definitionId=91&branchName=develop) | [![NuGet](https://img.shields.io/nuget/vpre/nanoFramework.System.Net.Sockets.TcpClient.svg?label=NuGet&style=flat&logo=nuget)](https://www.nuget.org/packages/nanoFramework.System.Net.Sockets.TcpClient/) |
22+
23+
## Usage
24+
25+
**Important:** Obviously this requires a working network connection. Please check the examples with the Network Helpers on how to connect to a network. For example see the [Networking sample pack](https://github.com/nanoframework/Samples/tree/main/samples/Networking)
26+
27+
The `TcpListener` class provides simple methods for creating a listening socket to accept incoming TCP connections and the `TcpClient` provides methods for connecting and communicating on a TCP connection.
28+
29+
### Samples
30+
31+
Samples for `TcpListener` and `TcpClient` are present in the [nanoFramework Sample repository](https://github.com/nanoframework/Samples).
32+
33+
### Listening for incoming connections
34+
35+
The following codes shows how to set up a Listening socket and to accept connections as a TcpClient on the 1234 port.
36+
37+
```csharp
38+
TcpListener listener = new TcpListener(IPAddress.Any, 1234);
39+
40+
// Start listening for incoming connections
41+
listener.Start();
42+
while (true)
43+
{
44+
try
45+
{
46+
// Wait for incoming connections
47+
TcpClient client = listener.AcceptTcpClient();
48+
49+
NetworkStream stream = client.GetStream();
50+
51+
Byte[] bytes = new Byte[256];
52+
int i;
53+
54+
// Wait for incoming data and echo back
55+
while((i = stream.Read(bytes, 0, bytes.Length))!=0)
56+
{
57+
// Do something with data ?
58+
59+
stream.Write(bytes, 0, i);
60+
}
61+
62+
// Shutdown connection
63+
client.Close();
64+
}
65+
catch(Exception ex)
66+
{
67+
Debug.WriteLine($"Exception:-{ex.Message}");
68+
}
69+
}
70+
```
71+
72+
If you want to handle more then one simultaneous connection then a separate worker thread can be started.
73+
74+
```csharp
75+
TcpListener listener = new TcpListener(IPAddress.Any, 1234);
76+
77+
// Start listening for incoming connections with backlog
78+
listener.Start(2);
79+
80+
while (true)
81+
{
82+
try
83+
{
84+
// Wait for incoming connections
85+
TcpClient client = listener.AcceptTcpClient();
86+
87+
// Start thread to handle connection
88+
Thread worker = new Thread(() => WorkerThread(client));
89+
worker.Start();
90+
}
91+
catch(Exception ex)
92+
{
93+
Debug.WriteLine($"Exception:-{ex.Message}");
94+
}
95+
}
96+
```
97+
98+
Worker Thread for handling the TcpClient connection for TcpListener example.
99+
100+
```csharp
101+
private static void WorkerThread(TcpClient client)
102+
{
103+
try
104+
{
105+
NetworkStream stream = client.GetStream();
106+
107+
Byte[] bytes = new Byte[256];
108+
int i;
109+
110+
// Loop reading data until connection closed
111+
while((i = stream.Read(bytes, 0, bytes.Length))!=0)
112+
{
113+
// Do something with data ?
114+
115+
// Write back received data bytes to stream
116+
stream.Write(bytes, 0, i);
117+
}
118+
}
119+
catch(Exception ex)
120+
{
121+
Debug.WriteLine($"Exception:-{ex.Message}");
122+
}
123+
finally
124+
{
125+
// Shutdown connection
126+
client.Close();
127+
}
128+
}
129+
```
130+
131+
### TcpClient
132+
133+
The TcpClient can also be used to initiate a connection passing in the hostname/port or IPEndPoint.
134+
Maybe connecting to another nanoFramework device which is listening for connections.
135+
136+
```csharp
137+
TcpClient client = new TcpClient()
138+
139+
try
140+
{
141+
client.Connect(hostname, port)
142+
143+
NetworkStream stream = client.GetStream();
144+
145+
// Write / Read data on stream
146+
147+
// for example Write 'ABC' and wait for response
148+
byte[] writeData = new byte[] { 0x41, 0x42, 0x43 };
149+
stream.Write(writeData, 0, writeData.Length);
150+
151+
// Read reply and close
152+
byte[] buffer = new byte[1024];
153+
int bytesRead = stream.Read(buffer, 0, buffer.Length);
154+
155+
// Process read data ?
156+
}
157+
catch(SocketException sx)
158+
{
159+
Console.WriteLine($"Socket error:{sx.ErrorCode} exception:{sx.Message}");
160+
}
161+
finally
162+
{
163+
client.Close();
164+
}
165+
```
166+
167+
For secure connections a `SslStream` can be used.
168+
169+
```csharp
170+
client.Connect(HostName, 443);
171+
172+
// Create SSlStream from underlying SOcket
173+
SslStream stream = new SslStream(client.Client);
174+
175+
// Don't verify Server certificate for this sample code
176+
stream.SslVerification = SslVerification.NoVerification;
177+
stream.AuthenticateAsClient(HostName, SslProtocols.Tls12);
178+
179+
// stream.Write() or stream.Read()
180+
```
181+
182+
## Feedback and documentation
183+
184+
For documentation, providing feedback, issues and finding out how to contribute please refer to the [Home repo](https://github.com/nanoframework/Home).
185+
186+
Join our Discord community [here](https://discord.gg/gCyBu8T).
187+
188+
## Credits
189+
190+
The list of contributors to this project can be found at [CONTRIBUTORS](https://github.com/nanoframework/Home/blob/main/CONTRIBUTORS.md).
191+
192+
## License
193+
194+
The **nanoFramework** Class Libraries are licensed under the [MIT license](LICENSE.md).
195+
196+
## Code of Conduct
197+
198+
This project has adopted the code of conduct defined by the Contributor Covenant to clarify expected behaviour in our community.
199+
For more information see the [.NET Foundation Code of Conduct](https://dotnetfoundation.org/code-of-conduct).
200+
201+
### .NET Foundation
202+
203+
This project is supported by the [.NET Foundation](https://dotnetfoundation.org).

0 commit comments

Comments
 (0)