{"id":19244334,"url":"https://github.com/nanoframework/system.net.sockets.tcpclient","last_synced_at":"2025-04-21T09:33:31.653Z","repository":{"id":37979387,"uuid":"454303734","full_name":"nanoframework/System.Net.Sockets.TcpClient","owner":"nanoframework","description":":package: System.Net.Sockets.TcpClient for .NET nanoFramework","archived":false,"fork":false,"pushed_at":"2025-04-02T12:19:13.000Z","size":210,"stargazers_count":4,"open_issues_count":0,"forks_count":3,"subscribers_count":5,"default_branch":"main","last_synced_at":"2025-04-19T17:14:53.318Z","etag":null,"topics":["csharp-library","dotnet","esp32","hacktoberfest","mcu","nanoframework","stm32","tcp-client"],"latest_commit_sha":null,"homepage":"https://www.nanoframework.net","language":"C#","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/nanoframework.png","metadata":{"funding":{"open_collective":"nanoframework","github":"nanoframework"},"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2022-02-01T07:57:34.000Z","updated_at":"2025-04-02T12:08:42.000Z","dependencies_parsed_at":"2023-02-08T08:02:06.545Z","dependency_job_id":"728c8658-c83f-40fd-a1b9-244e436e810b","html_url":"https://github.com/nanoframework/System.Net.Sockets.TcpClient","commit_stats":{"total_commits":129,"total_committers":7,"mean_commits":"18.428571428571427","dds":"0.45736434108527135","last_synced_commit":"3d49f8e087b65b2d4aa98d5ef74f2b9b06c44a5b"},"previous_names":[],"tags_count":59,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nanoframework%2FSystem.Net.Sockets.TcpClient","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nanoframework%2FSystem.Net.Sockets.TcpClient/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nanoframework%2FSystem.Net.Sockets.TcpClient/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nanoframework%2FSystem.Net.Sockets.TcpClient/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nanoframework","download_url":"https://codeload.github.com/nanoframework/System.Net.Sockets.TcpClient/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250032465,"owners_count":21363839,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["csharp-library","dotnet","esp32","hacktoberfest","mcu","nanoframework","stm32","tcp-client"],"created_at":"2024-11-09T17:22:58.280Z","updated_at":"2025-04-21T09:33:31.640Z","avatar_url":"https://github.com/nanoframework.png","language":"C#","funding_links":["https://opencollective.com/nanoframework","https://github.com/sponsors/nanoframework"],"categories":[],"sub_categories":[],"readme":"[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=nanoframework_System.Net.Sockets.TcpClient\u0026metric=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\u0026metric=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\u0026style=flat\u0026logo=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\u0026logoColor=white\u0026label=Discord\u0026color=7289DA)](https://discord.gg/gCyBu8T)\n\n![nanoFramework logo](https://raw.githubusercontent.com/nanoframework/Home/main/resources/logo/nanoFramework-repo-logo.png)\n\n-----\n\n# Welcome to the .NET **nanoFramework** System.Net.Sockets.TcpClient\n\nThis 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).\n\nThese are wrapper classes for the Socket when using TCP connections.\nThe nanoframework implementation of TcpClient doesn't include the asynchronous methods and the Connected property.\n\n## Build status\n\n| Component | Build Status | NuGet Package |\n|:-|---|---|\n| nanoFramework.System.Net.Sockets.TcpClient | [![Build Status](https://dev.azure.com/nanoframework/System.Net.Sockets.TcpClient/_apis/build/status/System.Net.Sockets.TcpClient?repoName=nanoframework%2FSystem.Net.Sockets.TcpClient\u0026branchName=main)](https://dev.azure.com/nanoframework/System.Net.Sockets.TcpClient/_build/latest?definitionId=93\u0026repoName=nanoframework%2FSystem.Net.Sockets.TcpClient\u0026branchName=main) | [![NuGet](https://img.shields.io/nuget/v/nanoFramework.System.Net.Sockets.TcpClient.svg?label=NuGet\u0026style=flat\u0026logo=nuget)](https://www.nuget.org/packages/nanoFramework.System.Net.Sockets.TcpClient/) |\n\n## Usage\n\n**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)\n\nThe `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.\n\n### Samples\n\nSamples for `TcpListener` and `TcpClient` are present in the [nanoFramework Sample repository](https://github.com/nanoframework/Samples).\n\n### Listening for incoming connections\n\nThe following codes shows how to set up a Listening socket and to accept connections as a TcpClient on the 1234 port.\n\n```csharp\nTcpListener listener = new TcpListener(IPAddress.Any, 1234);\n\n// Start listening for incoming connections\nlistener.Start();\nwhile (true)\n{\n    try\n    {\n        // Wait for incoming connections\n        TcpClient client = listener.AcceptTcpClient();\n\n        NetworkStream stream = client.GetStream();\n\n        Byte[] bytes = new Byte[256];        \n        int i;\n\n        // Wait for incoming data and echo back\n        while((i = stream.Read(bytes, 0, bytes.Length))!=0)\n        {\n            // Do something with data ?\n\n            stream.Write(bytes, 0, i);\n        }\n\n        // Shutdown connection\n        client.Close();\n    }\n    catch(Exception ex)\n    {\n        Debug.WriteLine($\"Exception:-{ex.Message}\");\n    }\n}\n```\n\nIf you want to handle more then one simultaneous connection then a separate worker thread can be started.\n\n```csharp\nTcpListener listener = new TcpListener(IPAddress.Any, 1234);\n\n// Start listening for incoming connections with backlog\nlistener.Start(2);\n\nwhile (true)\n{\n    try\n    {\n        // Wait for incoming connections\n        TcpClient client = listener.AcceptTcpClient();\n\n        // Start thread to handle connection\n        Thread worker = new Thread(() =\u003e WorkerThread(client));\n        worker.Start();\n    }\n    catch(Exception ex)\n    {\n        Debug.WriteLine($\"Exception:-{ex.Message}\");\n    }\n}\n```\n\nWorker Thread for handling the TcpClient connection for TcpListener example.\n\n```csharp\nprivate static void WorkerThread(TcpClient client)\n{\n    try\n    {\n        NetworkStream stream = client.GetStream();\n\n        Byte[] bytes = new Byte[256];        \n        int i;\n\n        // Loop reading data until connection closed\n        while((i = stream.Read(bytes, 0, bytes.Length))!=0)\n        {\n            // Do something with data ?\n\n            // Write back received data bytes to stream\n            stream.Write(bytes, 0, i);\n        }\n    }\n    catch(Exception ex)\n    {\n        Debug.WriteLine($\"Exception:-{ex.Message}\");\n    }\n    finally\n    {\n        // Shutdown connection\n        client.Close();\n    } \n}\n```\n\n### TcpClient\n\nThe TcpClient can also be used to initiate a connection passing in the hostname/port or IPEndPoint. \nMaybe connecting to another nanoFramework device which is listening for connections.  \n\n```csharp\nTcpClient client = new TcpClient()\n\ntry\n{\n    client.Connect(hostname, port)\n\n    NetworkStream stream = client.GetStream();\n\n    // Write / Read data on stream\n\n    // for example Write 'ABC' and wait for response\n    byte[] writeData = new byte[] { 0x41, 0x42, 0x43 };  \n    stream.Write(writeData, 0, writeData.Length);\n\n    // Read reply and close\n    byte[] buffer = new byte[1024];\n    int bytesRead = stream.Read(buffer, 0, buffer.Length);\n\n    // Process read data ?\n}\ncatch(SocketException sx)\n{\n    Console.WriteLine($\"Socket error:{sx.ErrorCode} exception:{sx.Message}\");\n}\nfinally\n{\n    client.Close();\n}\n```\n\nFor secure connections a `SslStream` can be used.\n\n```csharp\nclient.Connect(HostName, 443);\n\n// Create SSlStream from underlying SOcket\nSslStream stream = new SslStream(client.Client);\n\n// Don't verify Server certificate for this sample code\nstream.SslVerification = SslVerification.NoVerification;\nstream.AuthenticateAsClient(HostName, SslProtocols.Tls12);\n\n// stream.Write() or stream.Read()\n```\n\n## Feedback and documentation\n\nFor documentation, providing feedback, issues and finding out how to contribute please refer to the [Home repo](https://github.com/nanoframework/Home).\n\nJoin our Discord community [here](https://discord.gg/gCyBu8T).\n\n## Credits\n\nThe list of contributors to this project can be found at [CONTRIBUTORS](https://github.com/nanoframework/Home/blob/main/CONTRIBUTORS.md).\n\n## License\n\nThe **nanoFramework** Class Libraries are licensed under the [MIT license](LICENSE.md).\n\n## Code of Conduct\n\nThis project has adopted the code of conduct defined by the Contributor Covenant to clarify expected behaviour in our community.\nFor more information see the [.NET Foundation Code of Conduct](https://dotnetfoundation.org/code-of-conduct).\n\n## .NET Foundation\n\nThis project is supported by the [.NET Foundation](https://dotnetfoundation.org).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnanoframework%2Fsystem.net.sockets.tcpclient","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnanoframework%2Fsystem.net.sockets.tcpclient","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnanoframework%2Fsystem.net.sockets.tcpclient/lists"}