TCP/IP protocol

Source: Internet
Author: User

Repost TCP/IP protocol simple analysis

First, TCP/IP and IP are two different protocols. They are divided into different layers in the layer-7 network model. IP is the network layer protocol, and TCP is the transport layer protocol at the higher layer, TCP is built on the IP protocol, so TCP and IP are usually connected together to the TCP/IP protocol.

In Windows, TCP protocol stack data packets are 1460 bytes by default. If the data transmitted at a time exceeds this length, it is divided into several TCP data packets whose length is not greater than 1460 bytes, each data packet is assigned a sequnce number (equivalent to the sequence number of each data packet. the receiver can know the sequence of the data packet)

After that, the TCP data packet is wrapped in the Data header of the previous layer of IP, forming an IP data packet transmitted online (in fact, the last layer of Ethernet data packet is also included, and the network finally transmits Ethernet data packets ).

After the IP packet arrives at the destination, the receiving end first removes the packet header of the IP packet and obtains the TCP packet. Each time a TCP packet is received, the receiving end needs to return an ACK packet to the sending end to inform the sending end that the packet has been received. If the receiving end does not receive the ACK response of a certain packet within a certain period of time, this packet will be sent again to ensure that the data can be received by the receiving end (it is normal to lose data packets on the Internet, if there is no data packet re-transmission mechanism, it is difficult to ensure that all the data sent can be received by the receiver ).

Each TCP packet is composed of a packet header and actual data. The packet header contains the following main content:

L source port (2 bytes)

Sending port number

L destination port (2 bytes)

Receiver port number

The TCP packet header only contains the port numbers of both parties. The IP addresses of both parties are in the IP packet header, so there is no IP address in the packet header of the TCP packet.

L sequence number (4 bytes)

The sequence number of the data, indicating the Starting sequence number of the data in the current data packet. For example, the seq of the previous data packet is in hexadecimal format: DF D5 AA 3D, and the actual data length of the data packet is 16 bytes, then the seq of the next data packet must be added with 16 on the basis of the seq of the previous data packet, for: DF D5 AA 4D

L acknowledgement number (4 bytes)

The response sequence number after receiving a packet from the other party. If a packet sent by the other party is received, an ACK response packet must be returned. The acknowledgement number in the packet header is based on the seq and actual data length of the data sent by the other party, return seq + actual data length, indicating that the data packet has been received. After receiving this response, the other party knows that the packet has been received by the other party according to the numerical calculation. If the ACK response is not received, it means the packet has been lost on the Internet and needs to be resent.

L header length (1 byte)

Indicates the length of the TCP packet header. The length of the entire TCP packet minus the packet header can get the actual length of data transmitted by the TCP packet.

L flags (1 byte)

Each digit is a flag. The following are the main symbols:

ACK-Indicates that the packet is an ACK response packet, indicating that a packet from the other party is received. The packet is indicated by the acknowledgement Number of the packet header.

Psh-Indicates that this is a package with actual data.

SYN-Indicates that this is a connection data packet. Both parties need to communicate. The client always sends the SYN data packet to the server to establish a TCP connection.

Fin-Indicates that the communication ends and the connected data packets are removed.

 

The following describes the complete process of transmitting data over TCP. A client sends some data to the server as an example.

 

Figure 1. A complete TCP communication process

 

1. Establish a connection

TCP is a connection-oriented protocol. To communicate with the server, you must first establish a connection. First, both parties have an address, that is, the IP address and port number (IP: Port) identify each end of the communication, the client's IP: Port and Server IP: Port constitute a Socket socket. The connection is to establish a channel between the client's IP: Port and the server's IP: Port, and initialize some basic communication settings so that subsequent data communication can proceed normally.

1.1. The client sends SYN

The Client Always initiates a connection. First, the client sends the SYN packet to the server to establish a TCP connection.

Syn packets only have a TCP packet header, and there is no actual data.

Flags indicates the SYN position of the byte, indicating that it is a SYN packet.

The sequence number is a 4-byte data randomly generated by the client and serves as the starting sequence number of the client data for this connection, in the future, the sequence number of the data packets sent from the client to the server is incremented by the actual length of data transmitted each time. In this way, the sequence of the data packets is determined based on the sequence number of each data packet, this allows the receiver to splice data packets according to the sequence of data packets.

1.2. The server responds to ack and SYN

After the server receives the SYN from the client, it first needs to send an ACK packet to the client to indicate that it has received the packet.

The flags of the data packet indicates the ACK bit of the byte, indicating that it is an ACK response packet.

Acknowledgement number is set to the actual length of the received data packet seq + data packet, because the actual length of the received SYN is 0, however, TCP considers that the length of the Data actively sent when the actual length is 0 to 1, and SYN is the data packet actively sent by the client, therefore, the server sets acknowledgement number to seq + 1 of the received data packet.

TCP communication can be bidirectional. Once a connection is established, the server can also send data to the client.

Therefore, the server will also send a SYN packet to the client. the flags of the packet indicates the SYN position of the byte, indicating that it is a SYN Packet and a 4-byte data is randomly generated, the start sequence number of the data on the Communication Server.

In reality, the server combines the two data packets into one data packet. Both SYN and ACK are set, and sequence number and acknowledgement number are also set, and are sent back to the client as a data packet.

1.3. The client responds to ACK

After the client receives the SYN packet from the server, it must respond to an ACK packet to receive the packet. Similarly, the acknowledgement number of the ack packet is set to seq + 1 of the received data packet (the actual data length of the SYN and ACK data packets is also 0 ).

2. send and receive data from each other

After the communication parties establish a connection, they can transmit data packets to each other.

At one end of the sent data packet, set the PSH of the flags standard byte to indicate that the data packet has actual data.

Set the sequence number to the sequence number of the previous packet plus the length of the previous packet.

If the packet is also an ACK packet, the Ack is set and the acknowledgement number is set to send the packet to the other party.

After receiving the data packet sent by the recipient, the receiver must reply to the ACK packet. If data is sent to the recipient at the same time, the actual data packet can be sent together with the ACK data packet.

Before removing the connection, both parties can send and receive data to each other, and the data sequence is based on their respective sequence number.

In Figure 1, the data communication in the blue part represents this phase.

3. Remove the connection

After the data exchange between the two parties is completed, the connection needs to be removed to end the communication.

3.1. The client sends fin

The other party sends a FIN packet to terminate the communication and disconnects the communication.

The client sets the fin of the flags standard byte of the data packet to indicate that the data packet ends.

3.2. The server returns ack and fin

After receiving the fin data packet from the client, the server first responds to an ACK data packet and then sends a fin data packet. The server also ends the communication.

3.3. The client responds to ACK

The client responds to ack and receives the fin data from the server. The communication between the two parties ends.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.