|
1 | | -# System.Net.Sockets.TcpClient |
| 1 | + |
| 2 | +[](https://sonarcloud.io/dashboard?id=nanoframework_System.Net.Sockets.TcpClient) [](https://sonarcloud.io/dashboard?id=nanoframework_System.Net.Sockets.TcpClient) [](LICENSE) [](https://www.nuget.org/packages/nanoFramework.System.Net.Sockets.TcpClient/) [](https://github.com/nanoframework/Home/blob/main/CONTRIBUTING.md) [](https://discord.gg/gCyBu8T) |
| 3 | + |
| 4 | + |
| 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 | [](https://dev.azure.com/nanoframework/System.Net.Sockets.TcpClient/_build/latest?definitionId=91&branchName=main) | [](https://www.nuget.org/packages/nanoFramework.System.Net.Sockets.TcpClient/) | |
| 21 | +| nanoFramework.System.Net.Sockets.TcpClient (preview) | [](https://dev.azure.com/nanoframework/System.Net.Sockets.TcpClient/_build/latest?definitionId=91&branchName=develop) | [](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