Skip to content

Try “polymorphism”, “TCP handshake” or “git rebase”.

    Computer Basics / Networking 2: TCP, UDP and Ports

    TCP: The Promise

    A registered courier with a phone line. Before sending anything, the courier phones the receiver:

    Chapter 4 of 123 min read

    The Plain Picture#

    A registered courier with a phone line. Before sending anything, the courier phones the receiver: "Ready?" — "Ready." Every parcel is numbered. The receiver phones back to confirm what arrived. Missing parcels are sent again. The receiver unpacks them in number order, so the contents come out exactly as sent.

    The Real Name#

    TCP (Transmission Control Protocol) turns IP's unreliable packets into a reliable, ordered stream of bytes between two programs. It promises:

    1. Connection: both sides agree to talk before data flows (Ch 5).

    2. Reliability: lost data is resent (Ch 6).

    3. Order: bytes are delivered in the order they were sent (Ch 6).

    4. No duplicates (Ch 6).

    5. Flow and congestion control: the sender slows down if the receiver or the network can't keep up (Ch 7).

    HTTP (the web), database connections, SSH and email all run on TCP.

    A piece of TCP data in one packet is called a segment. A TCP header is at least 20 bytes.

    Under the Hood#

    TCP is a byte stream, not messages#

    This trips up many people. If StudyBuddy writes "Hello" and then "World", the receiver might read "HelloWorld" in one go, or "Hel" then "loWorld". TCP keeps the bytes in order but does not keep message boundaries.

    So every protocol built on TCP needs its own way to mark where a message ends — for example a length number at the start, or a special end marker. HTTP does this with headers (Networking 3: DNS, HTTP and HTTPS).

    TCP vs UDP#

    TCPUDP
    Connectionyes, handshake firstno
    Deliveryguaranteed (or you get an error)not guaranteed
    Orderguaranteednot guaranteed
    Message boundariesno — byte streamyes — each datagram is one message
    Slows down when neededyesno
    Header20+ bytes8 bytes
    Speed to first byteslower (handshake)immediate
    Used forweb, APIs, databases, SSHDNS, video/voice, games, QUIC

    Check yourself

    3 questions
    1

    List TCP's five promises.

    Show answer

    Connection, reliability, order, no duplicates, flow/congestion control.

    2

    If a program writes "A" then "B" over TCP, will the receiver always get two separate reads?

    Show answer

    No — TCP is a byte stream; it might arrive as "AB" in one read.

    3

    Why is TCP used for databases but UDP for video calls?

    Show answer

    Database data must be complete and exact; for video, low delay matters more than every packet.