appzasGamesToolsDevLearnCheat sheetsQuick calcsConvertersReferenceNetworkTimeCalculatorsCompareLLM prices

TCP and UDP

Lesson 1 of 411 minIntermediate

Reliable streams versus fast datagrams, and the handshake behind every connection.

IP only delivers packets from one address to another, with no guarantees. The transport layer adds ports and, in the case of TCP, reliability.

TCPUDP
ConnectionYes: a handshake firstNo: just send
DeliveryReliable and in order, with retransmissionBest effort: packets can be lost or reordered
OverheadHigherVery low
Used byHTTP(S), SSH, email, databasesDNS queries, video calls, games, QUIC (HTTP/3)

The TCP three-way handshake

  1. Client sends SYN ("I want to talk, my sequence starts at x").
  2. Server answers SYN-ACK ("Agreed, mine starts at y, I got x").
  3. Client sends ACK ("Got y"). The connection is established.

Only then does data flow. TLS adds its own handshake on top, which is why the first request to a new HTTPS server takes a few round trips.

What the failures look like

  • Connection refused: the host replied with a RST because nothing listens on the port.
  • Timeout: no reply at all. A firewall is probably dropping the packets, or the host is unreachable.
  • Connection reset: the other side closed abruptly, often a proxy or a crash.

NoteTCP also adapts to congestion: it slows down when packets are lost. That is why a lossy link makes TCP transfers much slower than the raw bandwidth suggests.

Test yourself

Answer all the questions, then check them. Finish with every answer right to mark the lesson as done.

1. What is the correct order of the TCP handshake?
2. A connection attempt hangs and then times out. What is most likely?
3. Which protocol is a good fit for a live video call?

Key terms