Tcp and udp
Tcp and udp
UDP header is 8-bytes fixed and simple header, while for TCP it may vary from 20 bytes to
60 bytes. First 8 Bytes contains all necessary header information and remaining part consist
of data.
UDP port number fields are each 16 bits long, therefore range for port numbers defined from
0 to 65535; port number 0 is reserved. Port numbers help to distinguish different user
requests or process.
1. Source Port : Source Port is 2 Bytes long field used to identify port number of
source.
2. Destination Port : It is 2 Bytes long field, used to identify the port of destined packet.
3. Length : Length is the length of UDP including header and the data. It is 16-bits field.
4. Checksum : Checksum is 2 Bytes long field. It is the 16-bit one’s complement of the
one’s complement sum of the UDP header, pseudo header of information from the IP
header and the data, padded with zero octets at the end (if necessary) to make a
multiple of two octets.
II. Introduction to TCP (TRANSMISSION CONTROL PROTOCOL)
It was specifically designed to provide a reliable end-to end byte stream over an unreliable
network. It was designed to adapt dynamically to properties of the inter network and to be
robust in the face of many kinds of failures.
Sockets:
A socket may be used for multiple connections at the same time. In
other words, 2 or more connections may terminate at same socket.
Connections are identified by socket identifiers at same socket. Connections
are identified by socket identifiers at both ends. Some of the sockets are listed
below:
Ports: Port numbers below 256 are called Well- known ports and are reserved
for standard services.
Transmission Control Protocol (TCP) Segment Header consists the following fields.
Source port: 16 Bit number which identifies the Source Port number (Sending Computer's
TCP Port).
Destination port: 16 Bit number which identifies the Destination Port number (Receiving
Port).
Sequence number: 32 Bit number used for byte level numbering of TCP segments. If you
are using TCP, each byte of data is assigned a sequence number.
If SYN flag is set (during the initial three way handshake connection initiation), then this is
the initial sequence number. The sequence number of the actual first data byte will then be
this sequence number plus 1. For example, let the first byte of data by a device in a particular
TCP header will have its sequence number in this field 50000. If this packet has 500 bytes of
data in it, then the next packet sent by this device will have the sequence number of 50000 +
500 + 1 = 50501.
Acknowledgment Number: 32 Bit number field which indicates the next sequence number
that the sending device is expecting from the other device.
Header Length: 4 Bit field which shows the number of 32 Bit words in the header. Also
known as the Data Offset field. The minimum size header is 5 words (binary pattern is 0101).
Control Bit Flags: We have seen before that TCP is a Connection Oriented Protocol. The
meaning of Connection Oriented Protocol is that, before any data can be transmitted, a
reliable connection must be obtained and acknowledged.
Control Bits govern the entire process of connection establishment, data transmissions and
connection termination. The control bits are listed as follows: They are:
ACK: Acknowledgement.
PSH: This flag means Push function. Using this flag, TCP allows a sending application to
specify that the data must be pushed immediately. When an application requests the TCP to
push data, the TCP should send the data that has accumulated without waiting to fill the
segment.
RST: Reset the connection. The RST bit is used to RESET the TCP connection due to
unrecoverable errors. When an RST is received in a TCP segment, the receiver must respond
by immediately terminating the connection. A RESET causes both sides immediately to
release the connection and all its resources. As a result, transfer of data ceases in both
directions, which can result in loss of data that is in transit. A TCP RST indicates an
abnormal termination of the connection.
SYN: This flag means synchronize sequence numbers. Source is beginning a new counting
sequence. In other words, the TCP segment contains the sequence number of the first sent
byte (ISN).
FIN: No more data from the sender. Receiving a TCP segment with the FIN flag does not
mean that transferring data in the opposite direction is not possible. Because TCP is a fully
duplex connection, the FIN flag will cause the closing of connection only in one direction.
To close a TCP connection gracefully, applications use the FIN flag.
Window: indicates the size of the receive window, which specifies the number of bytes
beyond the sequence number in the acknowledgment field that the receiver is currently
willing to receive.
Checksum: The 16-bit checksum field is used for error-checking of the header and data.
Urgent Pointer: Shows the end of the urgent data so that interrupted data streams can
continue. When the URG bit is set, the data is given priority over other data streams (Size 16
bits).
V. TCP Connection Establishment
1. SYN: The active open is performed by the client sending a SYN to the server. The
client sets the segment’s sequence number to a random value A.
2. SYN-ACK: In response, the server replies with a SYN-ACK. The acknowledgment
number is set to one more than the received sequence number (A + 1), and the
sequence number that the server chooses for the packet is another random number, B.
3. ACK: Finally, the client sends an ACK back to the server. The sequence number is set
to the received acknowledgement value i.e. A + 1, and the acknowledgement number
is set to one more than the received sequence number i.e. B + 1.
At this point, both the client and server have received an acknowledgment of the
connection. The steps 1, 2 establish the connection parameter (sequence number) for one
direction and it is acknowledged. The steps 2, 3 establish the connection parameter (sequence
number) for the other direction and it is acknowledged. With these, a full-duplex
communication is established.
1. One application calls close first, and we say that this end performs the active
close. This end's TCP sends a FIN segment, which means it is finished sending
data.
2. The other end that receives the FIN performs the passive close. The received FIN is
acknowledged by TCP. The receipt of the FIN is also passed to the application as an
end-of-file (after any data that may have already been queued for the application to
receive), since the receipt of the FIN means the application will not receive any
additional data on the connection.
3. Sometime later, the application that received the end-of-file will close its socket. This
causes its TCP to send a FIN.
4. The TCP on the system that receives this final FIN (the end that did the active close)
acknowledges the FIN.
Since a FIN and an ACK are required in each direction, four segments are normally
required. We use the qualifier "normally" because in some scenarios, the FIN in Step 1 is sent
with data. Also, the segments in Steps 2 and 3 are both from the end performing the passive
close and could be combined into one segment. We show these packets in Figure 2.3.
The working of the TCP sliding window mechanism can be explained as below.
The sending device can send all packets within the TCP window size (as specified in the TCP
header) without receiving an ACK, and should start a timeout timer for each of them.
The receiving device should acknowledge each packet it received, indicating the sequence
number of the last well-received packet. After receiving the ACK from the receiving device,
the sending device slides the window to right side.
VIII. TCP Congestion Control
Congestion in Network-
The size of the sender window is determined by the following two factors-
1. Receiver window size
2. Congestion window size
• Sender should not send data greater than receiver window size.
• Otherwise, it leads to dropping the TCP segments which causes
TCP Retransmission.
• So, sender should always send data less than or equal to receiver window size.
• Receiver dictates its window size to the sender through TCP Header.
2. Congestion Window-
• Sender should not send data greater than congestion window size.
• Otherwise, it leads to dropping the TCP segments which causes TCP Retransmission.
• So, sender should always send data less than or equal to congestion window size.
• Different variants of TCP use different approaches to calculate the size of congestion
window.
• Congestion window is known only to the sender and is not sent over the links.
So, always-
Sender window size = Minimum (Receiver window size, Congestion window size)
TCP’s general policy for handling congestion consists of following three phases-
• Initially, sender sets congestion window size = Maximum Segment Size (1 MSS).
• After receiving each acknowledgment, sender increases the congestion window size
by 1 MSS.
• In this phase, the size of congestion window increases exponentially.
When sender detects the loss of segments, it reacts in different ways depending on how the
loss is detected-
• Time Out Timer expires before receiving the acknowledgement for a segment.
• This case suggests the stronger possibility of congestion in the network.
• There are chances that a segment has been dropped in the network.