πŸ“¦ System.Net.Sockets.TcpClient for .NET nanoFramework
ΠŸΠ΅Ρ€Π΅ΠΉΡ‚ΠΈ ΠΊ Ρ„Π°ΠΉΠ»Ρƒ
nfbot d66701e441
Update 1 NuGet dependencies
***NO_CI***
2024-11-21 00:41:53 +00:00
.github/workflows Work CI-CD 2022-12-27 18:04:53 +00:00
Tests Update 1 NuGet dependencies 2024-11-21 00:41:53 +00:00
assets Miscellaneous fixes (#3) 2022-02-23 13:27:39 +00:00
config Updates for TcpClient repo (#2) 2022-02-23 23:08:39 +13:00
nanoframework.System.Net.Sockets.TcpClient Update 1 NuGet dependencies 2024-10-21 01:41:41 +01:00
.github_changelog_generator Updates for TcpClient repo (#2) 2022-02-23 23:08:39 +13:00
.gitignore Initial commit (#1) 2022-02-07 19:13:30 +13:00
CHANGELOG.md Update CHANGELOG for v1.1.77 2024-05-13 01:14:19 +00:00
LICENSE.md Initial commit (#1) 2022-02-07 19:13:30 +13:00
NuGet.Config Initial commit (#1) 2022-02-07 19:13:30 +13:00
README.md Work CI-CD 2022-03-30 10:44:17 +01:00
azure-pipelines.yml Work CI-CD 2024-01-20 18:32:31 +00:00
nanoframework.System.Net.Sockets.TcpClient.DELIVERABLES.nuspec Work CI-CD 2022-03-30 10:44:17 +01:00
nanoframework.System.Net.Sockets.TcpClient.nuspec Update 1 NuGet dependencies 2024-07-29 16:24:33 +01:00
nanoframework.System.Net.Sockets.TcpClient.sln Miscellaneous fixes (#3) 2022-02-23 13:27:39 +00:00
version.json Work CI-CD 2022-08-03 18:42:35 +01:00

README.md

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.