What is Transmission Control Protocol (TCP)?

In this tutorial, you will learn the basic concepts of Transport Control Protocol. After reading this tutorial, you will learn about the TCP header, TCP session establishment and termination, TCP service model, and uses of TCP.

Contents:

  1. Transmission Control Protocol (TCP)
  2. TCP Segment Header
  3. TCP Processes
  4. TCP Connection Establishment
  5. TCP Session Termination
  6. Use of Control Fields and Three-Way Handshake
  7. TCP Service Model
  8. Uses of TCP

Transmission Control Protocol (TCP)

TCP is one of the protocols of the transport layer. It is a reliable and connection-oriented protocol. Originally, it is designed to provide reliable communication over an unreliable network.

  • When TCP segments travel through the Internetwork, they pass through multiple paths and different types of networks. They dynamically optimize the properties of the internetwork, which prevents TCP segment failures.
  • TCP has the functionality to establish a session between sender and receiver. It provides reliable communication, same-order delivery, and flow control.
  • The transport layer divides the data into TCP segments so that they can fit into IP datagrams and Ethernet frames. The IP protocol uses a best-effort mechanism, so there is no guarantee that datagrams will be delivered properly; that’s all TCP determines.
  • TCP is reliable and slower than UDP because it has to manage and track segment information.
  • The TCP protocol provides the reliability that is needed for most applications. It is also used in the segmentation and reassembly of data.

TCP Segment Header

TCP is a stateful protocol which means that it manages and keeps track of the state information of each segment and session. TCP records that when it sends a segment and which segment was accepted. All details of the data are stored in the TCP header, which is 20 bytes.

When the transport layer receives data from the application layer, it adds a TCP header to the data to form a TCP segment.

advertisement
advertisement

The figure below shows the TCP header.

TCP header

As shown in the figure, the source and destination ports, sequence and acknowledgment numbers, header length, reserved and control bits, window, checksum, urgent, options, and application layer data are the fields of TCP headers.

  • Source and destination ports: The source and destination ports are 16 bits used to identify the source and destination application, respectively.
  • Sequence Number: The sequence number is a 32-bit field used on the receiver side to reassemble the data.
  • Acknowledgment Number: The acknowledgment number is a 32-bit field sent by the receiver to the sender to say that the data has been received.
  • Header Length: The Header Length field is of 4 bits which indicates the length of the TCP segment header.
  • CWR (Congestion Window Reduced) and ECE (ECN-Echo): Both fields are 1-bit flags used when ECN is used.
    • The ECE flag is set if the receiver wants to tell the sender to slow down the communication.
    • The CWR flag is set by the sender to let the receiver know that it has slowed down the rate of data transmission.
  • URG (Urgent Pointer): When urgent data is detected during communication that needs to be sent, the URG pointer is set to 1.
  • ACK: The ACK flag is 1 bit and is set to 1 if the acknowledgment number is valid. If the segment does not contain an acknowledgment number, it is set to 0.
  • PSH: PSH is the push data flag set to 1 when data needs to be pushed to the application on arrival.
  • RST: The RST bit is set when the connection needs to be reset due to a host crash or other reasons.
  • SYN: SYN is 1-bit and is used to establish a connection between the sender and the receiver.
  • FIN: The FIN flag is set if the connection needs to be terminated.
  • Window: This is the size of the window that tells how many bits need to be sent at once when a connection is established.
  • Checksum: Checksum is used to detect errors during transmission.
  • Options: The options field is variable in length, which is used to add additional features.

TCP Processes

When the client wants services from the server, a TCP connection is established between the client and the server. For that, the client and the server have manually assigned a port address by the network administrator or dynamically.

  • When a TCP connection is established between the client and the server, first, the client sends a TCP request to the server, which has the destination port set as the well-known port for the specific service. For example, port 80 is used for web services.
  • Here, the client generates the source port number dynamically.
  • When the server receives a client TCP request, it sets the client’s destination port number and the client’s source port address as its source port address and destination port address, respectively. In essence, the server reverses the source and destination port addresses of the client.
  • After that, the server responds to the client and provides services.

The figure below explains the TCP process.

TCP Process on a Network
  • As shown in the figure, the PC-1 dynamically sets the source port address as 48155. It wants web services from the server, so it sets the destination port as 80. Additionally, PC-2 also sets the source port address as 51155 and the destination address as 25, as it requires email services.
  • Both send TCP requests to the server. When the server receives requests from PC-1 and PC-2, it checks the destination port address and identifies the intended service of the client.
  • After that, the server reverses the source and destination port addresses and sends services to PC-1 and PC-2.

TCP Connection Establishment

When two friends meet, they greet each other by shaking hands to start a conversation. Similarly, on the network, when the client and the server want to communicate, they use a three-way handshake mechanism.

The figure below explains the TCP three-way handshake process.

TCP three-way handshake process
  • The client chooses a sequence number and sends the SYN flag to the server to initiate client-to-server communication.
  • When the server receives the SYN flag, it gives an acknowledgment to the client and requests a server-to-client session. Here, the ACK is set as the previous sequence number + 1, and the server generates a new sequence number.
  • Upon receiving the SYN and ACK fields from the server on the client-side, a connection is established between the client and the server.
  • This whole process is called a three-way handshake. Basically, it is used to check whether the destination host is available for communication or not. In our example, the server is available for communication.
  • The disadvantage of the three-way handshake is that the malicious sender can send multiple streams of SYN segments and can tie up the resources of the server. This attack is known as an SYN flood. This attack is prevented by SYN cookies.

advertisement

TCP Session Termination

When the communication process between the client and the server is completed, the connection needs to be terminated. The FIN (FINISH) flag is set in the TCP header to end the session. To terminate a TCP connection, there is a two-way handshake mechanism in which the FIN and ACK flags are used.

The below diagram explains the TCP session termination.

TCP session termination
  • As shown in the figure, four exchanges are needed to end the TCP session of the client and the server.
  • When the client completes its broadcast and wants to end the session, it sends a segment to the server with the FIN flag set.
  • The server sends an ACK to terminate the client-to-server session in response to the FIN flag.
  • After sending the ACK to the client, the server sends a FIN to terminate the server-to-client session.
  • Upon receiving a FIN from the server, the client sends an ACK as a response.
  • The session is closed after all segments have been exchanged.

Use of Control Fields and Three-Way Handshake

Hosts establish communication with each other using a three-way handshake mechanism. When a connection is established, the host maintains the state of the segment and tracks each TCP segment within a session.

  • The three-way handshake is used to identify whether the destination exists or is offline on the network.
  • The three-way handshake also verifies that the destination host has active services and accepts requests on the port number.
  • When the client wants to establish a connection on a specific destination port number, the three-way handshake informs the server that the client wants to establish communication on this port number.
  • Here, session establishment and session termination increase the reliability of the network.
  • To establish communication and exchange state information, the control bits flags (URG, ACK, PSH, RST, SYN, and FIN) are used.
  • The control field is a 6-bit region used to indicate the purpose and function of a TCP segment.

advertisement

TCP Service Model

When two devices want to communicate, they create endpoints for communication and transmission of data, known as sockets. A socket has an IP address and a port address. Whenever a connection is established, it is established between machine sockets.

  • The same socket is used for multiple simultaneous connections, which means that more than two connections can terminate in the same socket.
  • Each connection has an identifier, a socket identifier, which is used to identify the individual connection.
  • Port numbers are used to identify processes. Port numbers from 0 to 1023 are known as well-known ports that are used for privileged users. Ports 1024 to 49151 are used for non-privileged users.
  • TCP connections are full-duplex, and links between devices are point-to-point, but it does not support multicasting.
  • The transmission of data in a TCP connection is a byte stream, not a message stream.
  • When a process running on a machine at the application layer passes data to TCP at the transport layer, TCP can send it immediately or buffer it to collect a large amount of data to be sent at once.
  • Control fields on the network are used for TCP streams. For example, when an application receives high-priority data that must be processed immediately, the URG flag stops collecting data and broadcasts everything it has to that connection.

Uses of TCP

When TCP is used, it handles all the tasks associated with the segmentation of data, reliability, tracking and management of data, controlling flow, and reassembling of data. For reliable communication, the TCP protocol is used, which guarantees segment delivery. If the client does not have a flow and error control mechanism, it can use the TCP protocol because TCP handles flow and error control.

The below diagram shows the uses of TCP.

Uses of TCP
  • As shown in the figure, If a client wants to send an email, it uses SMTP port 25 in the TCP segment. To receive sent mail, IMAP 110 is used in the TCP segment.
  • FTP (20, 21) is used in the TCP segment if the client needs to transfer files to the server or another client.
  • Similarly, HTTP and HTTPS are used for web services, RTSP is used for media player control in the TCP segment.

Key Points to Remember

Here is the list of key points we need to remember about “Transmission Control Protocol (TCP)”.

  • TCP is one of the protocols of the transport layer. It is a reliable and connection-oriented protocol. Originally, it is designed to provide reliable communication over an unreliable network.
  • TCP has the functionality to establish a session between sender and receiver. It provides reliable communication, same-order delivery, and flow control.
  • The source and destination ports, sequence and acknowledgment numbers, header length, reserved and control bits, window, checksum, urgent, options, and application layer data are the fields of TCP headers.
  • On the network, when the client and the server want to communicate, they use a three-way handshake mechanism.
  • To terminate a TCP connection, there is a two-way handshake mechanism in which the FIN and ACK flags are used.
  • When two devices want to communicate, they create endpoints for communication and transmission of data, known as sockets. A socket has an IP address and a port address.
  • If the client does not have a flow and error control mechanism, it can use the TCP protocol because TCP handles flow and error control.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.