Study Material On TCP: TCP (Transmission Control Protocol)
Study Material On TCP: TCP (Transmission Control Protocol)
Rizvi
TCP is a stream-oriented protocol. UDP adds its own header to each of these
messages and delivers them to IP for transmission. Each message from the process
is calIed a user datagram and becomes, eventually, one IP datagram. Neither IP nor
UDP recognizes any relationship between the datagrams. TCP, on the other hand,
allows the sending process to deliver data as a stream of bytes and allows the
receiving process to obtain data as a stream of bytes. TCP creates an environment
in which the two processes seem to be connected by an imaginary "tube" that
carries their data across the Internet. The sending process produces (writes to) the
stream of bytes, and the receiving process consumes (reads from) them.
Concept of Segments
Although buffering handles the disparity between the speed of the producing and
consuming processes, we need one more step before we can send data. The IP
layer, as a service provider for TCP, needs to send data in packets, not as a stream
of bytes. At the transport layer, TCP groups a number of bytes together into a
packet called a segment. TCP adds a header to each segment (for control purposes)
and delivers the segment to the IP layer for transmission. The segments are
encapsulated in IP datagrams and transmitted. This entire operation is transparent
to the receiving process.
Full-Duplex Communication
TCP offers full-duplex service, in which data can flow in both directions at the
same time. Each TCP then has a sending and receiving buffer, and segments move
in both directions.
Connection-Oriented Service
TCP, unlike UDP, is a connection-oriented protocol. When a process at site A
wants to send and receive data from another process at site B, the following occurs:
Page 1 of 7
Prof.(Dr.) S. W. A. Rizvi
Note that this is a virtual connection, not a physical connection. The TCP segment
is encapsulated in an IP datagram and can be sent out of order, or lost, or
corrupted, and then resent. Each may use a different path to reach the destination.
There is no physical connection.
TCP creates a stream-oriented environment in which it accepts the responsibility of
delivering the bytes in order to the other site. The situation is similar to creating a
bridge that spans multiple islands and passing all the bytes from one island to
another in one single connection. We will discuss this feature later in the chapter.
Reliable Service
TCP is a reliable transport protocol. It uses an acknowledgment mechanism to
check the safe and sound arrival of data. We will discuss this feature further in the
section on error control.
Numbering System
Although the TCP software keeps track of the segments being transmitted or
received, there is no field for a segment number in the segment header.
Byte Number
TCP numbers all data bytes that are transmitted in a connection. Numbering is
independent in each direction. When TCP receives bytes of data from a process, it
stores them in the sending buffer and numbers them. The numbering does not
necessarily start from 0. Instead, TCP generates a random number between 0 and
232 - 1 for the number of the first byte. For example, if the random number happens
to be 1057 and the total data to be sent are 6000 bytes, the bytes are numbered
from 1057 to 7056.
Sequence Number
After the bytes have been numbered, TCP assigns a sequence number to each
segment that is being sent. The sequence number for each segment is the number
of the first byte carried in that segment.
Page 2 of 7
Prof.(Dr.) S. W. A. Rizvi
Example
Suppose a TCP connection is transferring a file of 5000 bytes. The first byte is
numbered 10,00l.What are the sequence numbers for each segment if data are sent
in five segments, each carrying 1000 bytes?
Solution
The following shows the sequence number for each segment:
Segment 1 Sequence Number: 10,001 (range: 10,001 to 11,000)
Segment 2 Sequence Number: 11,001 (range: 11,001 to 12,000)
Segment 3 Sequence Number: 12,001 (range: 12,001 to 13,000)
Segment 4 Sequence Number: 13,001 (range: 13,001 to 14,000)
Segment 5 Sequence Number: 14,001 (range: 14,001 to 15,000)
Acknowledgment Number
As we discussed previously, communication in TCP is full duplex; when a
connection is established, both parties can send and receive data at the same time.
Each party numbers the bytes, usually with a different starting byte number. The
sequence number in each direction shows the number of the first byte carried by
the segment. Each party also uses an acknowledgment number to confirm the bytes
it has received. However, the acknowledgment number defines the number of the
next byte that the party expects to receive. In addition, the acknowledgment
number is cumulative, which means that the party takes the number of the last byte
that it has received, safe and sound, adds I to it, and announces this sum as the
acknowledgment number. The term cumulative here means that if a party uses
5643 as an acknowledgment number, it has received all bytes from the beginning
up to 5642.
TCP Segment
The segment consists of a 20- to 60-byte header, followed by data from the
application program. The header is 20 bytes if there are no options and up to 60
bytes if it contains options.
Source port address. This is a 16-bit field that defines the port number of the
application program in the host that is sending the segment. This serves the same
purpose as the source port address in the UDP header.
Destination port address. This is a 16-bit field that defines the port number of the
application program in the host that is receiving the segment. This serves the same
purpose as the destination port address in the UDP header.
Page 3 of 7
Prof.(Dr.) S. W. A. Rizvi
Sequence number. This 32-bit field defines the number assigned to the first byte
of data contained in this segment. As TCP is a stream transport protocol. To ensure
connectivity, each byte to be transmitted is numbered.
TCP Segment
Acknowledgment number. This 32-bit field defines the byte number that the
receiver of the segment is expecting to receive from the other party. If the receiver
of the segment has successfully received byte number x from the other party, it
defines x + 1 as the acknowledgment number.
Header length. This 4-bit field indicates the number of 4-byte words in the TCP
header. The length of the header can be between 20 and 60 bytes. Therefore, the
value of this field can be between 5 (5 x 4 =20) and 15 (15 x 4 =60).
Control. This field defines 6 different control bits or flags. One or more of these
bits can be set at a time. These bits enable flow control, connection establishment
and termination, connection abortion, and the mode of data transfer in TCP.
Page 4 of 7
Prof.(Dr.) S. W. A. Rizvi
Flag Description
URG The value of the urgent pointer field is valid.
Window size. This field defines the size of the window, in bytes that the other
party must maintain. Note that the length of this field is 16 bits, which means that
the maximum size of the window is 65,535 bytes. This value is normally referred
to as the receiving window and is determined by the receiver. The sender must
obey the dictation of the receiver in this case.
Checksum. This 16-bit field contains the checksum. However, the inclusion of the
checksum in the UDP datagram is optional, whereas the inclusion of the checksum
for TCP is mandatory.
Urgent pointer. This l6-bit field, which is valid only if the urgent flag is set, is
used when the segment contains urgent data. It defines the number that must be
added to the sequence number to obtain the number of the last urgent byte in the
data section of the segment.
TCP Communication
TCP is connection-oriented. A connection-oriented transport protocol establishes a
virtual path between the source and destination. All the segments belonging to a
message are then sent over this virtual path. Using a single virtual pathway for the
entire message facilitates the acknowledgment process as well as retransmission of
damaged or lost frames.
Page 5 of 7
Prof.(Dr.) S. W. A. Rizvi
TCP uses the services of IP to deliver individual segments to the receiver, but it
controls the connection itself. If a segment is lost or corrupted, it is retransmitted.
Unlike TCP, IP is unaware of this retransmission. If a segment arrives out of order,
TCP holds it until the missing segments arrive; IP is unaware of this reordering.
In TCP, connection-oriented transmission requires three phases: connection
establishment, data transfer, and connection termination.
Connection Establishment
TCP transmits data in full-duplex mode. When two TCPs in two machines are
connected, they are able to send segments to each other simultaneously. This
implies that each party must initialize communication and get approval from the
other party before any data are transferred.
Three-Way Handshaking
The connection establishment in TCP is called three way handshaking, An
application program, called the client, wants to make a connection with another
application program, called the server, using TCP as the transport layer protocol.
The process starts with the server. The server program tells its TCP that it is ready
to accept a connection. This is called a request for a passive open.
Although the server TCP is ready to accept any connection from any machine in
the world, it cannot make the connection itself. The client program issues a request
for an active open. A client that wishes to connect to an open server tells its TCP
that it needs to be connected to that particular server. TCP can now start the three-
way handshaking process.
The client sends the first segment, a SYN segment, in which only the SYN flag is
set. This segment is for synchronization of sequence numbers. It consumes one
sequence number. When the data transfer starts, the sequence number is
incremented by 1.
The server sends the second segment, a SYN +ACK segment, with 2 flag bits set:
SYN and ACK. This segment has a dual purpose. It is a SYN segment for
communication in the other direction and serves as the acknowledgment for the
SYN segment.
The client sends the third segment. This is just an ACK segment. It acknowledges
the receipt of the second segment with the ACK flag and acknowledgment number
field. Note that the sequence number in this segment is the same as the one in the
SYN segment; the ACK segment does not consume any sequence numbers.
Page 6 of 7
Prof.(Dr.) S. W. A. Rizvi
Data Transfer
After connection is established, bidirectional data transfer can take place. The
client and server can both send data and acknowledgments.
Connection Termination
Any of the two parties involved in exchanging data (client or server) can close the
connection,
Three-Way Handshaking
Most implementations today allow three-way handshaking for connection
termination
1. In a normal situation, the client TCP, after receiving a close command from the
client process, sends the first segment, a FIN segment in which the FIN flag is set.
Note that a FIN segment can include the last chunk of data sent by the client.
The server TCP, after receiving the FIN segment, informs its process of the
situation and sends the second segment, a FIN +ACK segment, to confirm the
receipt of the FIN segment from the client and at the same time to announce the
closing of the connection in the other direction. This segment can also contain the
last chunk of data from the server.
The client TCP sends the last segment, an ACK segment, to confirm the receipt of
the FIN segment from the TCP server. This segment contains the acknowledgment
number, which is 1 plus the sequence number received in the FIN segment from
the server.
Page 7 of 7