TCP(Transmission Control Protcol)
TCP is a standard protocol with STD number 7. TCP is described by RFC 793 —Transmission Control Protocol.

"Connection-Oriented Service"
TCP provides considerably more facilities for applications than UDP, notably error recovery, flow control and reliability. TCP is a connection-oriented protocol unlike UDP which is connectionless. Most of the application layer protocols, such as Telnet, FTP, and HTTP use TCP.
The two processes communicate with each other over a TCP connection(InterProcess Communication - IPC), also called as "socket-to-socket communication"

Underlying best-effort network, IP may:
-Drop messages
-Re-orders messages
-Delivers duplicate copies of a given message
-Limits messages to some finite size
-Delivers messages after an arbitrarily long delay due to congestion of networks

So, to provide a connection-oriented service using IP, TCP needs to:
-End-to-end connection establishment & connection termination
-Guarantee message delivery
-Deliver messages(to the application layer/process) in the same order they are sent
-Deliver at most one copy of each message
-Support arbitrarily large messages
-Support full-duplex communication using Windowing for flow control
-Support multiple application processes on each host
-Efficient delivery of messags

Operations of TCP
-Stream transport--provides byte stream transfers between 2 machines through Client-Server communication(process-to-process communication using socket connections)
-The services provided by the socket-to-socket connection is called "Virtual circuit service"--connecton-oriented management
-Buffered transfer--for efficient transfers, data from application are segmented/accumulated into optimal sizes adjusted to the rate an application generates
-Provides full duplex connection
-Uses sliding window protocol with bytes as acknowledgement unit

TCP uses a connection; a virtual circuit between two application programs

Connections are identified by a pair of endpoints--an end point is called a "socket"
An endpoint is defined as a pair of integers ( H, P ) ;
H=IP address of host,
P=TCP port number

Example of connections:
Connection#1 : (132. 241. 158. 21. 1000) -- (120. 2. 3. 4. 23)---A telnet connection between a workstation with IP address of 132.241.158.21 and local ephemeral port number 1000 & a telnet server 120.2.3.4 and its well-known port number of 23(telnet)
Connection#2 : (132. 241. 158. 33. 2000) -- (120. 2. 3. 4. 23)---A telnet connection between a workstation with IP address of 132.241.158.33 and local ephemeral port number 2000 & a telnet server 120.2.3.4 and its well-known port number of 23(telnet)
A well-known TCP port number (e.g. port 23) can be reused by multiple connections on the same machine since TCP identifies a connection by a "pair of endpoints"
Each end point is called "Socket"-Therefore, socket-to-socket communication

TCP Packet format

Where:
Source Port
The 16-bit source port number, used by the receiver to reply.
Destination Port
The 16-bit destination port number.
Sequence Number
The sequence number of the first data byte in this segment. If the SYN control bit is set, the sequence number is the initial sequence number (n) and the first data byte is n+1.
Acknowledgment Number
If the ACK control bit is set, this field contains the value of the next sequence number that the receiver is expecting to receive.
Data Offset
The number of 32-bit words in the TCP header. It indicates where the data begins.
Reserved
Six bits reserved for future use; must be zero.
URG
Indicates that the urgent pointer field is significant in this segment.
ACK
Indicates that the acknowledgment field is significant in this segment.
PSH
Push function.
RST
Resets the connection.
SYN
Synchronizes the sequence numbers.
FIN
No more data from sender.
Window
Used in ACK segments. It specifies the number of data bytes beginning with the one indicated in the acknowledgment number field which the receiver (=the sender of this segment) is willing to accept.
Checksum
The 16-bit one's complement of the one's complement sum of all 16-bit words in a pseudo-header, the TCP header and the TCP data. While computing the checksum, the checksum field itself is considered zero.
The pseudo-header is the same as that used by UDP for calculating the checksum. It is a pseudo-IP-header, only used for the checksum calculation, with the format shown in the figure below.

TCP Pseudo-header format

Urgent Pointer
Points to the first data octet following the urgent data. Only significant when the URG control bit is set.
Options
One of the several options can be chosen

Flow control & Error control
TCP employs "a variable size byte-oriented windowing"--size of window is measured by bytes and the size can be adjusted dynamically by the receiver


Receiver sends "credit" (the #of additional bytes that the receiver is prepared to accept)
The size of credit varies according to receiver's current status(buffer availablity, load, ..)

Maximum segment size is negotiable between applications
On a LAN, TCP computes maximum size to match LAN's maximum transer unit(MTU)
On Internet, current spec. suggests maximum size of 536 ( the default size of IP datagram(576) - standard size of IP & TCP headers )
If Size too large ---> more fragmented ---> more overhead ---> low efficiency
If Size too small ---> higher overhead ratio(e.g. sending 1 character ---> 1/41(40 bytes for IP & TCP headers)

Optimum size is difficult to find because
-TCP has no mechanism to do that
-Routes change dynamically
-Optimum size also depends on lower-level protocol headers

ARQ
When a timeout occurs(a segment is not acknowledged by the receiver), each TCP implementation is free to react to a timeout as the implementers wish(choice between "Go-back-n" & "Selective-repeat) but most of the implementations chose "Selective-repeat" for effiecient transmission

Timeout period calculation
TCP records
-time for each segment sent
-time for the segment acknowledgement--the time when the acknowledgement for a segment arrives
-timeout periods are adjusted dynamically

Round Trip Time(RTT)  =  ( a * Old_RTT ) + (   ( 1 - a ) * New_RTT )
         where 0 <= a < 1    a   is called "smoothing factor" with a recommended value of 0.9
Then the New Timeout = b * RTT (where 1 <= b ) b is a "delay variance" with a recommended value of 2

Example: Given current RTT=250ms, a new RTT=70ms

RTT = 90%*250 + (1– 90% )*70=232

RTT calculation is not simple because TCP acknowledgement is ambiguous(There is no distinction between an ack to the original message and an ack to the retransmitted message(after timeout and resent))
So, Karn's algorithm: In RTT calculation, the RTTs for retransmitted segments are ignored but when a retransmission occurs then increase the Timeout by a given factor
( new_timeout = g * timeout where g is typically 2)

TCP connection establishment
3 way handshake

Process 1                                        Process2
                                                     Passive OPEN
                                                     Server waits for active request from client
Active OPEN
Send SYN, seq=n ---------------->
                                                     Receive SYN
                           <----------------Send SYN, seq=m, ACK n+1
Receive SYN+ACK
Send ACK m+1 ------------------>

Why 3 ways?
To avoid prob. of original & retransmitted request arrival(TCP ignores additional request during a connection phase)

TCP connection close--uses 3 way handshaking


Reserved(Well-know) TCP Port Numbers

# Keyword UNIX Description
-----------------------------------------------------------------------------
0 - - Reserved
1 TCPMUX - TCP Multiplexor
5 RJE - Remote Job Entry
7 ECHO echo Echo
9 DISCARD discard Discard
11 USERS systat Active Users
13 DAYTIME daytime Daytime
15 - netstat Network status program
17 QUOTE qotd Quote of the Day
19 CHARGEN chargen Character Generator
20 FTP-DATA ftp-data File Transfer Protocol
21 FTP ftp FTP control
23 TELNET telnet Terminal connection
25 SMTP smtp Simple Mail Transport Protocol
37 TIME time Time
42 NAMESERVER name Host Name Server
43 NICNAME whois Who Is
53 DOMAIN nameserver Domain Name Server
77 - rje any private rje service
79 FINGER finger Finger
93 DCP - Device Control Protocol
95 SUPDUP supdup SUPDUP Protocol
101 HOSTNAME hostnames NIC Host Name Server
102 ISO-TSAP iso-tsap ISO-TSAP
103 X400 x400 X.400 Mail Service
104 X400-SND x400-snd X.400 Mail Sending
111 SUNRPC sunrpc SUN Remote Procedure Call
113 AUTH auth Authentication Service
117 UUCP-PATH uucp-path UUCP Path Service
119 NNTP nntp USENET News Tranfer Protocol
129 PWDGEN - Password Generator Protocol
139 NETBIOS-SSN - NETBIOS Session Service
160-223 Reserved

Summary
TCP provides
Reliable delivery of byte streams
Full-duplex sliding window w/ credit scheme using byte windows
Flexible/robust to work over wide variety of network technology