TCP and UDP are transport protocols that carry internet traffic. TCP provides a reliable, ordered byte stream, or reports a connection failure; UDP sends independent datagrams without delivery or ordering guarantees.
TCP pays for reliability in time. UDP sends and moves on, leaving the app to cope with missing data.
Where TCP and UDP sit
Both are transport protocols. Underneath them, IP moves packets from one machine to another, but it promises nothing: a packet can be dropped, duplicated or arrive out of order, and IP won't tell anyone. The transport layer sits on top of IP (layer 4 in the OSI model) and decides how much of that mess the application has to deal with.
Both protocols add ports, so one machine can run a web server and a game server side by side and still hand each packet to the right program. After that, they make opposite choices. TCP (Transmission Control Protocol) hides the mess. UDP (User Datagram Protocol) passes it straight up to the application.
How TCP makes delivery reliable
TCP is connection-oriented: the two sides agree to talk before any data moves, and both keep track of what has been sent and received until the connection closes.
The handshake
A TCP connection starts with a three-way handshake:
- The client sends a SYN ('I want to connect, and here is where my numbering starts').
- The server replies with a SYN-ACK ('fine, here is mine, and I got yours').
- The client sends an ACK ('got yours too').
Only then can data flow. That is one full round trip before the first useful byte, which adds up across a slow mobile connection or a server on another continent.
Acknowledgements and resending
TCP numbers every byte it sends. The receiver replies with acknowledgements (ACKs) saying how far it has got. The sender keeps a copy of everything that hasn't been acknowledged, and if the confirmation doesn't come in time, it sends that data again.
Wondering how this was made? How we make these