nanoframework.System.Net.Sockets.TcpClient 1.1.114

Prefix Reserved
There is a newer version of this package available.
See the version list below for details.
dotnet add package nanoframework.System.Net.Sockets.TcpClient --version 1.1.114
                    
NuGet\Install-Package nanoframework.System.Net.Sockets.TcpClient -Version 1.1.114
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="nanoframework.System.Net.Sockets.TcpClient" Version="1.1.114" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="nanoframework.System.Net.Sockets.TcpClient" Version="1.1.114" />
                    
Directory.Packages.props
<PackageReference Include="nanoframework.System.Net.Sockets.TcpClient" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add nanoframework.System.Net.Sockets.TcpClient --version 1.1.114
                    
#r "nuget: nanoframework.System.Net.Sockets.TcpClient, 1.1.114"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#addin nuget:?package=nanoframework.System.Net.Sockets.TcpClient&version=1.1.114
                    
Install nanoframework.System.Net.Sockets.TcpClient as a Cake Addin
#tool nuget:?package=nanoframework.System.Net.Sockets.TcpClient&version=1.1.114
                    
Install nanoframework.System.Net.Sockets.TcpClient as a Cake Tool

Quality Gate Status Reliability Rating License NuGet #yourfirstpr Discord

nanoFramework logo


Welcome to the .NET nanoFramework System.Net.Sockets.TcpClient

This API implements the TcpListener and TcpClient classes with a pattern similar to the official .NET equivalent. System.NET.Sockets.TcpClient.

These are wrapper classes for the Socket when using TCP connections. The nanoframework implementation of TcpClient doesn't include the asynchronous methods and the Connected property.

Build status

Component Build Status NuGet Package
nanoFramework.System.Net.Sockets.TcpClient Build Status NuGet

Usage

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

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.

Samples

Samples for TcpListener and TcpClient are present in the nanoFramework Sample repository.

Listening for incoming connections

The following codes shows how to set up a Listening socket and to accept connections as a TcpClient on the 1234 port.

TcpListener listener = new TcpListener(IPAddress.Any, 1234);

// Start listening for incoming connections
listener.Start();
while (true)
{
    try
    {
        // Wait for incoming connections
        TcpClient client = listener.AcceptTcpClient();

        NetworkStream stream = client.GetStream();

        Byte[] bytes = new Byte[256];        
        int i;

        // Wait for incoming data and echo back
        while((i = stream.Read(bytes, 0, bytes.Length))!=0)
        {
            // Do something with data ?

            stream.Write(bytes, 0, i);
        }

        // Shutdown connection
        client.Close();
    }
    catch(Exception ex)
    {
        Debug.WriteLine($"Exception:-{ex.Message}");
    }
}

If you want to handle more then one simultaneous connection then a separate worker thread can be started.

TcpListener listener = new TcpListener(IPAddress.Any, 1234);

// Start listening for incoming connections with backlog
listener.Start(2);

while (true)
{
    try
    {
        // Wait for incoming connections
        TcpClient client = listener.AcceptTcpClient();

        // Start thread to handle connection
        Thread worker = new Thread(() => WorkerThread(client));
        worker.Start();
    }
    catch(Exception ex)
    {
        Debug.WriteLine($"Exception:-{ex.Message}");
    }
}

Worker Thread for handling the TcpClient connection for TcpListener example.

private static void WorkerThread(TcpClient client)
{
    try
    {
        NetworkStream stream = client.GetStream();

        Byte[] bytes = new Byte[256];        
        int i;

        // Loop reading data until connection closed
        while((i = stream.Read(bytes, 0, bytes.Length))!=0)
        {
            // Do something with data ?

            // Write back received data bytes to stream
            stream.Write(bytes, 0, i);
        }
    }
    catch(Exception ex)
    {
        Debug.WriteLine($"Exception:-{ex.Message}");
    }
    finally
    {
        // Shutdown connection
        client.Close();
    } 
}

TcpClient

The TcpClient can also be used to initiate a connection passing in the hostname/port or IPEndPoint. Maybe connecting to another nanoFramework device which is listening for connections.

TcpClient client = new TcpClient()

try
{
    client.Connect(hostname, port)

    NetworkStream stream = client.GetStream();

    // Write / Read data on stream

    // for example Write 'ABC' and wait for response
    byte[] writeData = new byte[] { 0x41, 0x42, 0x43 };  
    stream.Write(writeData, 0, writeData.Length);

    // Read reply and close
    byte[] buffer = new byte[1024];
    int bytesRead = stream.Read(buffer, 0, buffer.Length);

    // Process read data ?
}
catch(SocketException sx)
{
    Console.WriteLine($"Socket error:{sx.ErrorCode} exception:{sx.Message}");
}
finally
{
    client.Close();
}

For secure connections a SslStream can be used.

client.Connect(HostName, 443);

// Create SSlStream from underlying SOcket
SslStream stream = new SslStream(client.Client);

// Don't verify Server certificate for this sample code
stream.SslVerification = SslVerification.NoVerification;
stream.AuthenticateAsClient(HostName, SslProtocols.Tls12);

// stream.Write() or stream.Read()

Feedback and documentation

For documentation, providing feedback, issues and finding out how to contribute please refer to the Home repo.

Join our Discord community here.

Credits

The list of contributors to this project can be found at CONTRIBUTORS.

License

The nanoFramework Class Libraries are licensed under the MIT license.

Code of Conduct

This project has adopted the code of conduct defined by the Contributor Covenant to clarify expected behaviour in our community. For more information see the .NET Foundation Code of Conduct.

.NET Foundation

This project is supported by the .NET Foundation.

Product Compatible and additional computed target framework versions.
.NET Framework net is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.1.122 170 4/24/2025
1.1.121 145 4/24/2025
1.1.120 175 4/2/2025
1.1.118 190 3/10/2025
1.1.117 167 3/10/2025
1.1.116 175 3/10/2025
1.1.115 127 3/3/2025
1.1.114 117 2/27/2025
1.1.112 110 2/25/2025
1.1.110 98 2/25/2025
1.1.107 125 2/5/2025
1.1.106 112 2/4/2025
1.1.105 116 2/4/2025
1.1.104 112 1/30/2025
1.1.103 104 1/30/2025
1.1.99 102 1/27/2025
1.1.97 138 1/6/2025
1.1.96 107 1/6/2025
1.1.95 106 1/6/2025
1.1.94 131 1/2/2025
1.1.83 188 7/29/2024
1.1.77 150 5/13/2024
1.1.74 169 4/8/2024
1.1.72 129 4/3/2024
1.1.69 178 1/29/2024
1.1.67 128 1/26/2024
1.1.64 269 11/10/2023
1.1.62 136 11/9/2023
1.1.59 133 11/9/2023
1.1.52 526 12/28/2022
1.1.50 344 12/28/2022
1.1.48 333 12/27/2022
1.1.45 344 12/23/2022
1.1.37 587 10/26/2022
1.1.35 414 10/26/2022
1.1.33 442 10/26/2022
1.1.31 417 10/25/2022
1.1.29 412 10/24/2022
1.1.27 450 10/24/2022
1.1.25 434 10/23/2022
1.1.23 438 10/23/2022
1.1.20 459 10/8/2022
1.1.17 468 9/22/2022
1.1.15 485 9/22/2022
1.1.13 474 9/15/2022
1.1.8 438 8/4/2022
1.1.6 440 8/4/2022
1.1.4 444 8/3/2022
1.1.2 439 8/3/2022
1.0.0.23 471 7/20/2022
1.0.0.21 475 6/13/2022
1.0.0.19 474 6/8/2022
1.0.0.14 467 5/26/2022
1.0.0.12 463 5/18/2022
1.0.0.10 463 5/3/2022
1.0.0 496 3/29/2022
1.0.0-preview.12 160 3/29/2022
1.0.0-preview.9 159 3/17/2022
1.0.0-preview.7 151 3/14/2022
1.0.0-preview.5 153 3/14/2022
1.0.0-preview.3 159 2/23/2022