TCP protocol

The Transport Control Protocol (TCP) is a Transport layer Protocol that provides reliable host-to-host data transmission and supports full duplex.

2. Host-to-host

1. TCP provides host-to-host transmission. One Host sends data To another Host through TCP.

A Host can be a mobile phone, a tablet, a watch, etc. Both are equal.

2. TCP is an application-to-application protocol.

To achieve host-to-host communication, TCP needs To know the network Address (IP Address) of the hosts. However, TCP is not responsible for address-to-address transmission. Therefore, TCP transfers IP addresses To the underlying Internet layer. Host-to-host provides inter-application communication capabilities for applications.

3. What are connections and sessions?

  • A connection is a contract between the data transfer parties.
  • A connection is a record of the behavior state of a network.

A Connection is an agreement between two communicating parties. The goal is to create a tacit understanding between two communicating programs to ensure that both programs are online and respond to each other’s requests as soon as possible. This is a Connection.

A session is the behavior of an application. Sessions are responsible for preserving state over multiple connections, such as AN HTTP Session that maintains state (such as user information) between multiple HTTP requests (connections).

Session is an application layer concept and connection is a transport layer concept.

4. Duplex/simplex problem

  • Data can only be sent in one direction, which is simplex. Simplex requires at least one line.
  • Data can be transmitted in one direction, or in the other direction and in the opposite direction, alternating, called half duplex; At least one line is required for half duplex.
  • If data can be sent and received in both directions at any time, this is full duplex, and full duplex requires more than one line.

TCP is a duplex protocol. Data can be transmitted in both directions at any time. This means that the client and server can send and receive information equally. Because of this, the client and server have an equal term in TCP — Host.

5. Reliability

Reliability index guarantees lossless transmission. If the sender sends the data sequentially, and then the data is sent out of order across the network, there must be an algorithm at the receiver to restore the data to the original order. In addition, if a sender wants to send a message to more than one recipient at the same time, a situation called multicast, reliability requires that each recipient receive the same copy without loss. The multicast case also has strong reliability, meaning that if a message reaches any recipient, all recipients must receive the message.

6. TCP handshake and wave

6.1 Basic TCP Operations

TCP has the following basic operations:

  • If a Host initiates a connection to another Host, this is called SYN (Synchroniation).
  • If a Host actively disconnects the request, called FIN (Finish), the request is complete;
  • If one Host sends data to another, it is called PSH (Push), and the data is pushed.

In the above three cases, the receiver needs to send an ACK response to the sender after receiving the data. The request/response model is a requirement for reliability, and if a request does not respond, the sender may think it needs to resend the request.

6.2 Establishing a Handshake (Three handshakes)

Because of connection and reliability constraints, TCP ensures that every piece of data sent must be returned, which is called an ACK (or response).

Simplify:

  • Client sends a message to the server (SYN)
  • The server is ready to connect
  • The server sends an ACK for the client’s SYN

This completes two handshakes, but it is not enough because the server has not yet determined whether the client is ready, so the following procedure needs to be added.

  • The server sends a SYN to the client
  • The client is ready
  • The client sends an ACK to the server

6.3 Disconnection Process (4 wave hands)

  • The client requests to disconnect and sends a request to disconnect, called (FIN).
  • The server receives the request and sends an ACK to the client in response to the FIN.
  • So here’s a question you need to think about, can you send the FIN back as soon as you shake hands? In fact, the server cannot send the FIN immediately because there are many problems to be handled. For example, the server may not receive ACK messages. It is also possible that the server itself has resources to release. So disconnecting cannot operate like a handshake – merging two messages. So the server waits until it is ready to close the connection and sends a FIN to the client.
  • The client receives a FIN from the server and needs to handle its own tasks. For example, if the client sends an ACK request to the server that does not receive an ACK, the client sends an ACK to the server.

conclusion

  • TCP provides connections to ensure stable and secure transmission.
  • TCP does not directly provide sessions because applications have various requirements on sessions. For example, chat programs maintain chat records of both parties, and e-commerce programs maintain consistency of shopping carts and orders. Therefore, sessions are usually further encapsulated on the TCP connection and provided at the application layer.
  • TCP is a connection-oriented Protocol. It means that TCP hosts establish connections before sending and receiving data. You will also learn about UDP. UDP is a Datagram-oriented protocol that directly transmits packets (data) without establishing a connection.
  • Finally, connections consume more resources. For example, a connection must be negotiated before data can be transferred. Therefore, not every scenario should use a connection-oriented protocol. In a scenario like video playback, it is unreasonable to use a connection-oriented protocol that requires the server to respond every time the server pushes a frame of video to the client.

The title

Why does TCP have 3 handshakes and 4 waves?

TCP is a duplex protocol. When establishing a connection, both parties need to send SYC (synchronous request) and ACK (response) to each other.

Neither party is doing too much work during the handshake, so after one party synchronizes (SYN) to the other, the other party can respond with its ACK and SYN as a single message, thus a three-way handshake — three data transfers.

By the waving stage, both sides may have unfinished business. The party receiving the wave request must respond immediately (ACK) to indicate that the wave request has been received. In the real world, when you receive an Offer, you respond to it out of politeness, and then think about it for a while before replying to HR with the final result. Finally, wait until all work is done, and then send a request to interrupt the connection (FIN), hence four waves.