.NET TCP API

Overview

The System.Net.Sockets.TcpClient and System.Net.Sockets.TcpListener classes provide a simple way to implement TCP network communication in .NET applications. They support both synchronous and asynchronous operations, IPv4/IPv6, and SSL/TLS encryption.

Classes

TcpClientProvides client connections for TCP network services.
TcpListenerListens for incoming TCP connection requests.
NetworkStreamProvides the underlying stream of data for TCP sockets.

Common Methods

  • TcpClient.ConnectAsync(IPAddress, int) – Asynchronously connects to a remote host.
  • TcpListener.Start() – Begins listening for incoming connections.
  • TcpListener.AcceptTcpClientAsync() – Accepts an incoming connection asynchronously.
  • NetworkStream.ReadAsync(byte[], int, int) – Reads data from the stream.
  • NetworkStream.WriteAsync(byte[], int, int) – Writes data to the stream.

Example: Echo Server & Client