Tuesday, March 31, 2020

TCP Segment and Header

TCP provides a connection-oriented data transmission between two or more hosts, can support multiple data streams, and provides for flow and error control, and even for the reordering of packets that may have been received out of order.

The TCP protocol header shown above is a minimum of 20 bytes and contains the following fields:
Source Port:  The 16-bit source port field contains the number of the port making the "call".  The source port and the source IP address function as the packet's return address.
Destination Port:  The 16-bit destination port field is the address of the "called" or destination port.  The IP address is used to forward the packet to the correct destination machine.  At this point, the TCP destination port is used to forward the packet to the correct application on that machine.
Sequence Number:  The 32-bit sequence number is used by the receiving machine to reconstruct the fragmented data back into its original form.  In a dynamically routed network, it is quite possible for some of the packets to take different routes and, consequently, arrive out of order.  This sequencing field compensates for any inconsistency in delivery.

Acknowledgement Number:  TCP uses a 32-bit "piggybacked" acknowledgement (ACK) of the next expected byte.  The number used to identify each ACK is the sequence number of the packet being acknowledged.
Data Offset:  This 4-bit field contains the size of the TCP header, measured in 32-bit words.
Reserved:  This 6-bit field is always set to zero.

Flags:  
TCP uses a series of flags to indicate whether the packet is supposed to initiate a connection, contain data, or terminate a connection.  The flags, in the order they appear, are: Urgent (URG), Acknowledge (ACK), Push (PSH), Reset (RST), Synchronize (SYN), and Finish (FIN).

Flag        Meaning
URG            Implies that there is urgent data in the packet that should receive priority processing.
ACK            Acknowledgement of successfully received data.
PSH             Request to immediately process any received data.
RST             Immediately terminates the connection.
SYN             Request to start a new connection.
FIN              Request to finish a connection.

Window Size (16 bits):  This field is used by the destination machine to tell the source host how much data it is willing to accept, per TCP segment.
Checksum (16 bits):  The TCP header also contains an error checking field known as a checksum.  The source host calculates a mathematical value, based upon the segment's contents.  This is called the Cyclic Redundancy Check (CRC).  The destination host performs the same calculation.  If the content remained intact, the result of the two calculations is identical, thereby proving the validity of the data.
Urgent Pointer (16 bits):  This is a valid field only if the URG pointer in the Flags field is set.  If so, this value indicates the offset from the current sequence number, in bytes, where the segment of non-urgent data begins.
Options:  May be zero, meaning that no options need to be present, or a multiple of 32 bits.  However if any options are used that do not cause the option field to total a multiple of 32 bits, padding of 0s should be used to make sure the data begins on a 32-bit boundary.  These boundaries are known as words.
Data:  Handed down to the TCP protocol at the Transport layer, which includes the upper-layer headers.

Transmission Control Protocol (TCP) takes large blocks of information from an application and breaks them into segments.  It numbers and sequences each segment so that the destination's TCP stack can put the segments back into the order the application intended.  After these segments are sent on the transmitting host, TCP waits for an acknowledgement of the receiving end's TCP virtual circuit session, retransmitting any segments that are not acknowledged.

Before a transmitting host starts to send segments down the model, the sender's TCP stack contacts the destination's TCP stack to establish a connection.  This creates a virtual circuit, and this type of communication is known as connection-oriented.  During this initial handshake, the two TCP layers also agree on the amount of information that's going to be sent before the recipient's TCP sends back an acknowledgement.   With everything agreed upon in advance, the path is paved for reliable communication to take place.

TCP is a full-duplex, connection-oriented, reliable, and accurate protocol, but establishing all these terms and conditions, in addition to error checking, is no small task.  TCP is very complicated, and so, not surprisingly, it's costly in terms of network overhead.  And since today's networks are much more reliable than those of yore, this added reliability is often unnecessary.  Most programmers use TCP because it removes a lot of programming work, but for real-time video and VoIP, User Datagram Protocol (UDP) is often better because using it results in less overhead.


No comments:

Post a Comment