TCP

Why
TCP or transmission control protocol is important because it provides a lot of extra features to processes than UDP

What
TCP is a transport layer protocol that provides the following services:
 * process-to-process data delivery
 * error detection
 * reliable data transfer
 * congestion control

Sequence Number and Acknowledgement Number
These fields are used by TCP sender and receiver to implement the reliable data transfer service

Receive Window
This 16 bit field is used for flow control. It indicates how many bytes the receiver is willing to accept

Header Length
This 4 bit field denotes how many 32 bit words are in the header

Options
This field is used for when a sender and receiver are negotiating the maximum segment size

Flag
The flag field contains 6 bits
 * ACK - denotes acknowledgement of a receipt
 * RST, SYN, FIN - handles connection setup and teardown
 * PSH - denotes that the transport layer should push the bits to the application layer immediately
 * URG - denotes whether the application layer marked any data as urgent. The pointer to the urgent data is captured in the 16 bit urgent data pointer

Connection Oriented
TCP is regarded as connection-oriented because we must go through a three-step handshaking phase before any data can actually flow between two hosts. However, we note that TCP is not circuit switched because only the two hosts at the end know about the TCP connection

Full Duplex
If there is a TCP connection between process A on a host and process B on another host, then the hosts can send information to each other at the same time

Point to Point
TCP connections are always between only two processes on two hosts

TCP Connection Management
TCP sets up connections through the following steps: TCP breaks down connections through the following steps:
 * 1) Client side TCP sends a special TCP segment with the SYN flag set to true and also with a randomly chosen initial sequence number
 * 2) Server side TCP allocates TCP buffer for the connection and other variables. Then it sends an acknowledges segment with SYN set to 1 and also with the acknowledgement number set to the initial sequence number + 1. This segment is referred to as the SYNACK segment
 * 3) Upon receiving the SYNACK segment, the client also allocates buffers. Then, the client acknolwedges receipt of this packet and sets the acknowledgement number to server number + 1 and sets the SYN bit to 0. This segment may carry data to the server
 * 1) Client TCP sends a TCP segment with the FIN flag set to 1
 * 2) Server TCP sends a TCP segment with the FIN flag set to 1
 * 3) Client TCP sends an ACK

Round Trip Estimation Time
In order to implement reliable data delivery, we must have some timer value that we wait before retransmitting. We simply take samples of the RTT with segments that we select and then we mix them into our belief about the average round trip time like so

$$RTT = (1 - \alpha) \times RTT + \alpha \times SampleRTT$$

We also need some sort of measurement of the deviation of RTT

$$DevRTT = (1 - \beta) \times DevRTT + \beta \times |SampleRTT - RTT|$$

With these values, we use the below formula to set the amount of time on the timer to wait before sending retransmissions

$$Timeout = RTT + 4 \times DevRTT$$

TCP Timer
The TCP is restarted in the following scenarios
 * Timeout. When this happens, the timeout is doubled
 * Receipt of an ACK. When this happen, the timeout is set to the value computed with RTT and DevRTT
 * Data received from application above. If the timer is not already running for another packet, then the timer is restarted

Congestion Control
The receiver keeps a variable called the receiver window which denotes how much remaining space it has in the receiving buffer. Because TCP cannot overflow the receiving buffer, it is important that it maintains this variable

When the receiver window size reaches 0, we still send 1 byte messages so that we will know when some space has cleared up in the receiving buffer