Safe and ordered, or fast and light? Two ways to send data.
TCP and UDP are two ways to send data between computers. Both sit on top of IP and both use ports to find the right app.
Data on the internet travels in small pieces called packets. A transport protocol decides how those packets are sent between two apps. TCP and UDP are the two main ones. They live at Layer 4 (the Transport layer) of the OSI model.
The network below (IP) only does its best. Packets can be lost, repeated, delayed or arrive in a different order. We need rules on top.
| Point | TCP | UDP |
|---|---|---|
| Connection | Yes (handshake) | No |
| Reliable delivery | Yes, resends lost data | No |
| Order kept | Yes | No |
| Speed | Slower (more checks) | Faster |
| Header size | 20 bytes or more | 8 bytes |
| Flow / congestion control | Yes | No |
| Message style | A continuous stream of bytes | Separate messages (datagrams) |
| Best for | Web, files, email, payments | Calls, games, live video, DNS |
| Step | TCP socket | UDP socket |
|---|---|---|
| Server starts | listen() then accept() | bind() to a port |
| Client starts | connect() (handshake) | No connect needed |
| Send | send() on the connection | sendto(address, data) |
| Receive | recv() gets a stream | recvfrom() gets one datagram |
| End | close() (FIN messages) | close() |
| Term | Meaning in easy words |
|---|---|
| Packet | A small piece of data sent over the network |
| Datagram | One self-contained UDP message |
| Port | A number (0 to 65535) that tells which app on a computer should get the data |
| Handshake | The first few messages two sides exchange to start a connection |
| SYN / ACK / FIN | TCP flags: start, โI got itโ, and finish |
| Sequence number | A number on each byte so the receiver can order the data |
| Retransmission | Sending a lost packet again |
| Flow control | Stopping a fast sender from flooding a slow receiver |
| Congestion control | Sending less when the network is busy |
| Packet loss | A packet that never arrives |
| Head-of-line blocking | In TCP, one lost packet makes later packets wait, even if they have arrived |
| QUIC | A newer protocol on top of UDP, used by HTTP/3 |
| Choice | We gain | We lose |
|---|---|---|
| TCP | Correctness | Some speed and some delay |
| UDP | Speed and low delay | Guarantees (you build them if you need them) |
| QUIC (over UDP) | Fast start, no head-of-line stall, built-in encryption | More complex to build; some networks block UDP |