TCP
Transmission Control Protocol
In short
TCP is a core internet protocol that delivers data between two programs reliably and in order, by opening a connection and resending anything that gets lost.
What is TCP?
TCP, the Transmission Control Protocol, is one of the main protocols of the internet. It creates a reliable connection between two programs, such as a browser and a web server, and guarantees that the bytes sent by one side arrive at the other complete, uncorrupted, and in the same order. TCP runs on top of IP: IP moves individual packets between machines, and TCP turns them into a dependable stream of data.
A TCP connection starts with a three-way handshake: the client sends SYN, the server replies with SYN-ACK, and the client answers with ACK. After that, data is split into numbered segments, and the receiver sends acknowledgments for what it has received. If a segment is not acknowledged in time, the sender retransmits it, while flow control and congestion control slow the sender down when the receiver or the network is overloaded.
A good analogy is a phone call. You first connect, then speak in order, and the other person can ask you to repeat anything they missed. TCP carries most traffic that must arrive exactly as sent, including web pages over HTTP/1.1 and HTTP/2, email, file transfers, SSH sessions, and database connections.
TCP is most often compared with UDP. UDP sends independent packets with no connection, no ordering, and no retransmission, which makes it faster and lighter but unreliable. Choose TCP when every byte must arrive correctly, and UDP when speed matters more than perfection, as in live video calls or online games.
At a glance
Key takeaways
- TCP is connection-oriented: it opens a connection with a three-way handshake before sending data.
- It guarantees delivery, correct order, and error checking using sequence numbers and acknowledgments.
- Lost data is detected and retransmitted automatically.
- Flow control and congestion control keep the sender from overwhelming the receiver or the network.
- The trade-off is extra overhead and latency compared with UDP.
Example
import socket
# Open a TCP connection to a web server on port 80
with socket.create_connection(("example.com", 80)) as sock:
# Send a minimal HTTP request over the TCP stream
sock.sendall(b"GET / HTTP/1.1\r\nHost: example.com\r\nConnection: close\r\n\r\n")
# TCP delivers the reply bytes reliably and in order
reply = sock.recv(1024)
print(reply.decode(errors="replace").splitlines()[0]) # e.g. HTTP/1.1 200 OKReaders ask
What is the difference between TCP and UDP?
TCP sets up a connection and guarantees that data arrives complete and in order, resending anything lost. UDP just sends packets with no connection or guarantees, which is faster but means some data may be lost or arrive out of order.
What is the TCP three-way handshake?
It is how a TCP connection starts: the client sends a SYN message, the server replies with SYN-ACK, and the client confirms with ACK. After these three steps, both sides have agreed on starting sequence numbers and can exchange data.
Does HTTP use TCP?
HTTP/1.1 and HTTP/2 run over TCP, usually with TLS on top for HTTPS. HTTP/3 instead runs over QUIC, a newer protocol built on UDP that adds its own reliability and encryption.
Often compared
See also
- UDPNetworking, p. 35UDP is a lightweight internet protocol that sends small, independent messages called datagrams without a connection, favoring speed over guaranteed delivery.
- IP AddressNetworking, p. 10An IP address is a numeric label assigned to each device on a network so that data can be routed to it, much like a postal address for a house.
- PacketNetworking, p. 20A packet is a small, formatted unit of data sent across a network, made of a header with addressing information and a payload that carries the actual data.
- PortNetworking, p. 22A port is a number from 0 to 65535 that identifies a specific program or service on a device, so traffic reaching an IP address gets to the right application.
- HTTPWeb Development, p. 19HTTP is the protocol that browsers, apps, and servers use to exchange web pages and data through a simple cycle of requests and responses.
- LatencyNetworking, p. 14Latency is the delay between sending a request and the start of a response, usually measured in milliseconds, and it shapes how responsive an app feels.
- TCP HandshakeNetworking, p. 31The TCP handshake is the SYN, SYN-ACK, ACK exchange a client and server use to open a connection and agree on starting sequence numbers before sending data.
Sources
Spotted a mistake or something missing on this page?Suggest an edit