TCP (Transmission Control Protocol)

What is TCP?

TCP is a layer 4 protocol. The Protocol Data Unit (PDU) of TCP is called segment.
It is a full-duplex protocol so both sender and receiver need a window for receiving messages from one another. 
It is responsible for end-to-end delivery and error correction. Before this, the packet should know where it should go. 
Pulling the network cable will not break a TCP connection(1) though it will disrupt communications. You can plug the cable back in and once IP connectivity is established, all back-data will move. This is what makes TCP reliable, even on cellular networks.
When TCP sends data, it expects an ACK in reply. If none comes within some amount of time, it re-transmits the data and waits again. The time it waits between transmissions generally increases exponentially.
After some number of retransmissions or some amount of total time with no ACK, TCP will consider the connection "broken". How many times or how long depends on your OS and its configuration but it typically times-out on the order of many minutes.


TCP 3-way Handshake 

it is prevalent since it provides reliability for the connection establishment. 
A device which initiates the connection is called Client. The device, to which the client connecting to, is called server. 
1. Client sends a segment with SYN (Synchronise Sequence Number) to establish a connection with server. SYN signifies with what sequence number it likely to start the segments with. 
2. a)If the data unit is received at the receiver's end is not damaged, it will send SYN/ACK to the client. 
Receiver will check the data using the checksum in the packet which is used for the error detection. 
2. b) If the data unit is received at the receiver's end is damaged, receiver will discard the segment. 
3. If 2 a) happens, client will send ACK message. If 2 b) happens, client will resend the data for which the positive acknowledgement is not received. 
4. Once they both established reliable connection with which they will start the actual data transfer. 



How TCP works?

Connection Establishment

Both Client/Sender & Server/Receiver is associated with port numbers. 
Let's say the sender's IP and port are 192.168.0.147 & 56208 
Receiver's IP and port are 192.168.192.10 & 13 

1. Usually, the sequence number will be picked up randomly by the client. In our case, it is 521. It will also set the SYN bit in the packet. This SYN bit will tell that it's a new connection. 
MSS field is present inside the option field. It is the Maximum Segment size, so the receiver sends segment which doesn't require fragmentation. It is 1460Bytes.
Window size is the sender's buffer capacity on which he has to receive the data. 
2. For the response, ACK bit will be set. It will also have the ack no as 522 to tell the client that it expects the next sequence number as 522 and this is the acknowledgement for the previous sequence 521. Server will set its own SYN bit, and it will have the random sequence number (it can be same also). In our case, it is 2000. 
Since the receiver's window size is smaller than sender, both parties agree to the minimum window size 500B. 
Therefore, receiver can send maximum of 29 (14600/500) packets. This is receiver's sending window size. 
But, receiver window size is 10000B, so sender can send (10000/500) 20 packets. 
3. Client will acknowledge with an ack bit set and will have the ack number as 2001. Client will set the sequence number as 522 as requested by server. 
With these three packets back and forth the connection is established. Now, both client and server can send data back and forth.
Sliding window allows the sender to transmit multiple segments within the TCP window size without receiving an acknowledgement. 
A device can send successive segments without waiting for an acknowledgement that a previous segment has been received.  

Connection Termination

Connections are full duplex, that is, two distinct channels from server to client and from client to server. Either side independently closes its channel. A close is signaled by the FIN flag. The FIN packet is ACK'ed with a sequence number one higher (FIN takes a sequence number).
To close both channels takes four messages, a FIN, its ACK, then the other side's FIN, and its ACK. Here are four scenarios:
  1. Half close: Client (or server) sends FIN, and Server ACK's the FIN. Server continues to send data. Eventually the server sends a FIN.
  2. Full close: Client (or server) sends FIN, and Server immediately ACK's and informs application of client close. Server requests a close so the next packet is a FIN to the client. Client ACK's Server's FIN.
  3. Simultaneous close: Both sides simultaneously send FIN packets. Both sides will respond with ACK's and the connection is fully closed.


After full closure, a TCP connection is required to wait for twice the maximum segment lifetime, called the 2MSL wait. This prevents old packets confusing new connections, if a new connection is immediately created using old port and IP numbers. It also aids in completing the close. The sender of the last ACK does not know if the ACK was recieved. The last ACK is not ACK'ed, by definition of being last. If a re-FIN is not received in 2MSL, it can be assumed that the last ACK was heard and accepted.


If the ACK is not received within a timeout interval, the data is retransmitted.

Reference

Utube 
GFG

Comments