Dev Learning Lab

The labs on this site need JavaScript to run. The same content is written out below.

Learn › Networking › Transport layer

TCP connections and retransmission

When a packet is lost, who notices and sends it again?

Why it exists — An IP network only delivers packets on a best-effort basis, and does not tell you if they vanish on the way, change order, or arrive in duplicate. TCP has the operating system handle numbering, acknowledgments, retransmission, and reordering, so that an application can exchange data like reading and writing a file, without taking care of loss.

Where it's used today — When you open a Socket in Java, connect to a DB with JDBC, or call another server with RestClient, a TCP connection is made underneath. Making a connection takes a round-trip time, so connections made earlier are reused through a connection pool or keep-alive. Voice calls and real-time games, where arriving late is worse than losing a little, mainly use UDP.

TCP establishes a connection before it sends data, and it receives acknowledgments that tell it how far the data has been received. If no acknowledgment arrives, it sends the data again. UDP sends without any of that procedure.

Checked against documentation and standards · Checked against the TCP specification (RFC 9293)

Concepts in this lab

TCP and UDP
The two main ways of transporting data on the internet. TCP establishes a connection before communication, guarantees that data arrives complete and in order, and sends data again if there is a problem. UDP sends data without a connection and does not guarantee arrival or order, but in return it is lightweight and fast. HTTP/1.1, HTTP/2, and database connections use TCP, and real-time video and voice and DNS mainly use UDP.
Three-way handshake
Why it exists — If data is sent right away without confirming the connection, it is sent without knowing whether the other side is ready to receive, or where to start counting the numbers attached to each piece. The three-way handshake lets both sides confirm each other's starting numbers in three exchanges, which sets the basis for counting missing pieces and order afterward.
Where it's used today — Developers do not call it directly, but the connection timeout is the limit on how long to wait for this process to finish. If a firewall silently drops the SYN, there is no reply and the wait lasts until this timeout, and if no program is waiting on the other side's port, Connection refused appears right away.
The three-step process used to establish a TCP connection. The client sends a connection request (SYN), the server sends its acceptance together with its own connection request (SYN+ACK), and the client acknowledges it (ACK). After both sides have confirmed that they can send to and receive from each other, data transfer starts.
Flow control and congestion control
Both are TCP functions that adjust the sending rate. Flow control adjusts the amount sent to match what the receiver can handle, so that the receiver does not overflow. Congestion control senses how congested the network is and decreases or increases the amount sent, to prevent congestion across the whole network.

Try these

Running with the default settings

  1. This is the first message of the three-way handshake. The client sends a SYN, which says "I want to connect." It announces the starting number (100) that it will use.
  2. This is the second message. The server answers, "Received (ACK), and I will connect too (SYN)." ack=101 means "I have received up to 100, so send 101 next."
  3. This is the third message. The client acknowledges the SYN of the server. Both sides have now confirmed that "what I send arrives, and what the other side sends reaches me." The connection is established.
  4. It splits the data into segments, numbers them, and sends them.
  5. The server acknowledges: "I have received up to 106, so send 107 next."
  6. It sends the second segment.
  7. It sends the third segment.
  8. The server acknowledges: "I have received everything up to 115."
  9. When there is nothing left to send, each side sends "I am finished (FIN)," has it acknowledged, and closes the connection.

Result of this run

Code

// TCP: connect first, then send (only the lines inside a method are shown; class, method declaration, imports, and the setup of data omitted)
Socket socket = new Socket("shop.example", 443);   // the three-way handshake happens here
OutputStream out = socket.getOutputStream();
out.write(data);                               // the operating system splits it into segments and
                                               // handles acknowledgment, retransmission, and ordering
socket.close();                                // close the connection

Assumptions of this model

This is a small example in which "Hello, world!!!" is split into three pieces and sent. The numbers (seq, ack) are arbitrary values chosen to make the example easier to follow, and each character is counted as 1 byte (ASCII text), so the three pieces are 6, 6, and 3 bytes. The timing of retransmission is simplified. Besides the timer expiring, real TCP also retransmits right away when the same acknowledgment arrives three times in a row (three duplicate ACKs). The principles of the three-way handshake and of retransmission follow the TCP specification (RFC 9293).

Previous lab: Sockets and ports · Next lab: TCP and UDP

Concepts in this lab

What this lab shows

Settings

Code


      
    

Diagram

Run comparison

Try these