TCP connection is used to establish a reliable and secure communication between two hosts. It can be used for any kind of data transfer, such as HTTP or FTP. The TCP protocol has four major components:
How do two hosts communicate reliably?
The figure below illustrates an example of a TCP connection between two hosts.
Each host has a sequence number that keeps track of the data transmission. The first number is called the initial sequence (ISN), and it’s chosen randomly by each side when they establish a connection. The ISN increases by one with every byte sent from one side to another until it reaches 4,294,967,295 bytes (232). After this point, it wraps around and starts again from zero—this is called “wrap-around” behavior.
8 steps to establish a TCP connection
- The client sends a SYN packet to the server.
- The server sends back a SYN-ACK packet to the client, acknowledging that it received their request for connection and is ready to receive data from that connection.
- The client sends back an ACK packet confirming that it has received these two packets and is now ready to exchange data with this particular connection as well as all of its multiplexed connections (multiple TCP sessions). At this point, we have made sure that both sides agree on how they want to communicate with each other: they’re negotiating an agreement over what type of communication protocol they will be using (packet size, header format, etc.). This process is known as “three-way handshake”. 4) Once both parties have agreed on everything necessary for establishing a TCP session (outlined above), then actual transmission of information can begin after sending FIN bit in every sent segment (note: SND_SSH(n) means segment n; SND_SEQ(m) means sequence number m). 5) When a host wants to close up its connection or send no more data through it, it uses FIN bit which means “finish” or “end.”
Step 1: the client sends a SYN packet to the server
The client sends a SYN packet to the server. This is the first message in establishing a TCP connection, and it contains:
- The destination IP address of the server (the IP address of your web browser)
- The destination port number on that server where HTTP requests will be sent (port 80)
- A sequence number that identifies your TCP connection among other connections that are already established with the same host (this allows packets belonging to different connections between two hosts to be distinguished).
Step 2: the server sends a SYN-ACK packet to the client
After Step 1, the client is ready to receive data. However, the server will not send any data until it receives a SYN-ACK packet from the client. This step is an essential part of TCP connection establishment: it confirms that both parties are ready to start exchanging data and that all necessary components are working as expected.
The process looks like this:
- The client sends a SYN packet to establish a connection with the server
- The server receives this SYN packet from the client and responds by sending back another SYN-ACK packet confirming its receipt of step 1’s request (which also indicates that steps 3 through 5 should happen)
Step 3: the client sends an ACK packet to the server, and now TCP connection is established.
The TCP connection is now established.
The client sends an ACK packet to the server, and now TCP connection is established.
TCP is a connection-oriented protocol, which means that a three-way handshake must take place before communication can start. The client sends SYN (synchronize) packets to the server and waits for an ACK packet from it before sending data back; this process continues until both sides have acknowledged each other’s existence (SYN, SYN+ACK, Acknowledgement).
Step 4: data transmission begins.
When the client sends a packet, it includes in it certain information about itself (such as its source address and port number) and the data that is being sent. The server does the same when it receives a packet from the client.
The header of each packet contains information about where the packet is coming from and where it’s going, so when your computer sends an email to Google or plays a game on Steam, its IP address appears in every packet send to those sites — even if you’ve never visited them before.
The payload contains all of your email messages or game controller input for example!
Step 5: when the client has sent all data, it will send a FIN packet to the server, for no more data will be sent from client side.
When the client has sent all data, it will send a FIN packet to the server, for no more data will be sent from client side. The server then acknowledges receipt of this packet and sends an ACK packet back. Now both sides are closed and ready for new connections if needed.
Step 6: after receiving the FIN packet from client, the server will send an ACK packet, and then close its write pipe.
No more data will be sent from server side.
Oops! Click Regenerate Content below to try generating this section again.
Step 7: server will also send a FIN packet to ask for ending this TCP connection.
Once the client sends an ACK packet, it will close its write pipe and no more data will be sent from either side. The TCP connection is now closed gracefully.
Step 8: after receiving the FIN packet from server, the client will send an ACK packet, and then close its write pipe.
No more data will be sent by either side. A TCP connection is closed gracefully.
Now, after receiving the FIN packet from server, the client will send an ACK packet to server and then close its write pipe. No more data will be sent by either side. A TCP connection is closed gracefully.
When a client or server sends a FIN packet, it means they are closing their end of the connection and no longer sending any more data. The other side needs to acknowledge this by sending an ACK packet before ending their part of the communication as well.