Transmission Control Protocol (TCP)
The Transmission Control Protocol (TCP) is one of the core protocols of the Internet protocol suite (IP), and is so common that the entire suite is often called TCP/IP. TCP provides reliable, ordered, error-checked delivery of a stream of octets between programs running on computers connected to a local area network, intranet or the public Internet. It resides at the transport layer.
Web browsers use TCP when they connect to servers on the World Wide Web, and it is used to deliver email and transfer files from one location to another.
Applications that do not require the reliability of a TCP connection may instead use the connectionless User Datagram Protocol (UDP), which emphasizes low-overhead operation and reduced latency rather than error checking and delivery validation.
FTP, Telnet, HTTP, HTTPS, SMTP, POP3, IMAP, SSH, and a variety of other protocols are typically encapsulated in TCP.
The protocol corresponds to the transport layer of TCP/IP suite. TCP provides a communication service at an intermediate level between an application program and the Internet Protocol (IP). That is, when an application program desires to send a large chunk of data across the Internet using IP, instead of breaking the data into IP-sized pieces and issuing a series of IP requests, the software can issue a single request to TCP and let TCP handle the IP details.
TCP protocol operations may be divided into three phases. Connections must be properly established in a multi-step handshake process (connection establishment) before entering the data transfer phase. After data transmission is completed, the connection termination closes established virtual circuits and releases all allocated resources.
Why we use TCP connection?
We use the Transmission Control Protocol (TCP) connection when we need reliable, ordered, and error-checked communication between devices over a network.
Here’s what TCP provides and why it matters:
1. Reliable delivery
TCP makes sure data actually reaches the destination.
• If packets are lost, TCP retransmits them.
• The receiver confirms received packets using acknowledgments (ACKs).
Example:
• File downloads
• Emails
• Web pages loading
Without reliability, files or messages could arrive incomplete.
2. Data arrives in order
Internet packets can take different routes and arrive out of sequence.
TCP rearranges packets correctly before giving data to the application.
Example:
If you stream a document or webpage, sentences must appear in the correct order.
3. Error checking
TCP checks whether data became corrupted during transmission using checksums.
If corruption is detected:
• the damaged packet is discarded
• the sender retransmits it
4. Flow control
TCP prevents a fast sender from overwhelming a slow receiver.
It dynamically adjusts transmission speed using the window size mechanism.
5. Congestion control
TCP helps avoid overloading the network.
Algorithms like:
• Slow Start
• Congestion Avoidance
• Fast Recovery
reduce traffic when congestion is detected.
Key Features of TCP
1. Connection-Oriented
Before data transfer, TCP establishes a connection using a three-way handshake.
2. Reliable Data Transfer
Uses acknowledgments (ACKs) and retransmissions to ensure data integrity.
3. Ordered Delivery
Packets are reassembled in the correct sequence, even if they arrive out of order.
4. Flow Control
Prevents overwhelming the receiver by adjusting the rate of data transmission.
5. Congestion Control
Adjusts sending rate based on network conditions to avoid congestion.
6. Error Detection
Uses checksums to detect corrupted data.
C# Examples of TCP Server/Client
1. TCP Server Example
This server:
• listens on port 5000
• accepts a client connection
• receives a message
• sends a response back
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
class TcpServer
{
static void Main()
{
TcpListener server = new TcpListener(IPAddress.Any, 5000);
server.Start();
Console.WriteLine("Server started...");
Console.WriteLine("Waiting for client...");
TcpClient client = server.AcceptTcpClient();
Console.WriteLine("Client connected!");
NetworkStream stream = client.GetStream();
byte[] buffer = new byte[1024];
int bytesRead = stream.Read(buffer, 0, buffer.Length);
string message = Encoding.UTF8.GetString(buffer, 0, bytesRead);
Console.WriteLine("Received: " + message);
string response = "Hello from server";
byte[] responseBytes = Encoding.UTF8.GetBytes(response);
stream.Write(responseBytes, 0, responseBytes.Length);
client.Close();
server.Stop();
}
}
2. TCP Client Example
This client:
• connects to the server
• sends a message
• receives the response
using System;
using System.Net.Sockets;
using System.Text;
class TcpClientExample
{
static void Main()
{
TcpClient client = new TcpClient();
client.Connect("127.0.0.1", 5000);
Console.WriteLine("Connected to server");
NetworkStream stream = client.GetStream();
string message = "Hello server";
byte[] data = Encoding.UTF8.GetBytes(message);
stream.Write(data, 0, data.Length);
byte[] buffer = new byte[1024];
int bytesRead = stream.Read(buffer, 0, buffer.Length);
string response = Encoding.UTF8.GetString(buffer, 0, bytesRead);
Console.WriteLine("Server says: " + response);
client.Close();
}
}
Real Applications Using TCP in C#
TCP in C# is commonly used for:
• chat applications
• multiplayer games
• IoT communication
• remote device control
• custom protocols
• microservices
• database servers
Advantages of TCP
• High reliability: Ensures no data loss (or retransmits if needed)
• Error recovery: Automatically handles packet loss
• Ordered delivery: Important for applications like web pages and file transfers
• Widely supported: Standard protocol across networks
Disadvantages of TCP
• Slower than UDP: Extra overhead from acknowledgments and error checking
• Higher latency: Connection setup (handshake) adds delay
• More resource usage: Requires memory and processing for connection management
• Not ideal for real-time apps: Delays can affect streaming, gaming, or VoIP
TCP vs UDP (Quick Context)
TCP is often compared to User Datagram Protocol:
• TCP → reliable but slower
• UDP → faster but unreliable