The TCP/IP protocol is usually put together, but they are two different protocols, and the role is not the same. IP protocol is used to find the address, corresponding to the Internetwork Interconnect layer, TCP protocol is used to standardize the transmission rules, corresponding to the transport layer. IP is only responsible for finding the address, the specific transfer of work to the TCP to complete, which is like courier delivery, the manifest on the address of the rules and how to find the customer according to the content, which is equivalent to the IP protocol, and delivery to Call first, and then send the goods to the past, the final signature of the customer to sign and so on is equivalent to the TCP protocol.
TCP in the transmission before the three communication, commonly known as the "three-time handshake", the data is broken when the time to carry out four communication, commonly known as "four Wave". To understand this process, you first need to understand the meaning of the two ordinal and three flag bits in TCP:
- The abbreviation for seq:sequence number that represents the ordinal of the data being transmitted. TCP transmission Each byte has a sequence number, the data will be sent when the first sequence number is sent to the other party, the receiver will be ordered by ordinal check whether the reception is complete, if not received the complete will need to retransmit, so that the data can be guaranteed Integrity.
- The abbreviation for ack:acknoledgement number, which indicates the acknowledgement. The receiver uses it to give the sender feedback of the data that has been successfully received, its value is the next packet to receive the starting sequence number, that is, the ACK value represented by the serial number before the data has been successfully received.
- ACK: The ACK bit, which only works when ack= 1. The ACK is 1 for normal communication, and the first time the request is initiated because there is no need to confirm the received data so the ACK is 0.
- SYN: The synchronization bit used to synchronize the sequence number when a connection is established. Just start to establish the connection when there is no historical data received, so the ACK can not be set, at this time according to the normal mechanism can not be run, the role of SYN is to solve this problem, when the receiver received the syn= 1 message Will directly set the ACK to receive the value of seq+ 1, note that the value here is not set after the calibration, but according to the SYN directly set, so that the normal mechanism can be run, so the SYN is called synchronous bit. It is important to note that SYN will be 1 for the first two handshakes, because both sides of the communication need to set an initial value for the ACK.
- FIN: The terminating bit, which is used to release the connection after the data transfer is complete.
The entire transmission process.
The top of the figure is three handshake, the lower part is four waves, here the four times the wave is the client to terminate the connection, in the actual transmission process may also be the service side to terminate the connection, their processing is the same. TCP transmission is a dual-mode, that is, the two sides of the transmission are equivalent, can transmit data at the same time, so whether the connection or shutdown need to both sides simultaneously. The first two in the three-time handshake guarantees that the server can receive and return the request correctly, and the last two times ensures that the client can receive and return the request correctly, and that the ACK value of both sides is initialized with the SYN flag during the three handshake. Four times the wave is the two sides send the FIN flag to close the connection and let the other side confirm.
Three times handshake and four waves to ensure the reliability of the connection, but there are pros and cons, this model also has its shortcomings, first of all in the transmission efficiency will be relatively low, and the other three handshake process during the client needs to send two data to establish a connected Then, this feature may be used by some ulterior motives, for example, the first handshake (and received a second handshake) will not respond to the third handshake, then the service side will assume that the second handshake data is lost in the transmission process, and then heavy New to send a second handshake, by default, will be sent five times, if sent five times after the third handshake will discard the request, if it is a separate request of course it does not matter, but everything is afraid of a multi-word, when there are a large number of such When the request is in trouble, then the server will waste a lot of resources may even lead to the inability to handle the normal request, this is a DDOS attack in the SYN Flood attack, one way to deal with this attack is to set the second request The number of times (tcp_ Synack_ retries), but the number of times is too small can cause the normal request because the network did not receive a second handshake and connection failure situation, the specific set to how appropriate, but also need to be based on the actual situation Judge, of course, if the funds are sufficient can also use hard defense.
Protocol for the Transport layer in addition to TCP and UDP, their differences are mainly TCP is connected, UDP is not connected, that is, the TCP protocol is communicated well before the data, and the UDP protocol is to get the address straight The result of this is that the data transmitted by the TCP protocol is more reliable, and the UDP is transmitted more quickly. TCP is like a phone call, you need to dial the other number to communicate, and UDP is like the use of walkie-talkie, pick up can speak directly. Usually the video transmission, voice transmission and so on the integrity requirements are not high and the transmission speed is high and the data volume of communication using UDP more, and the mail, Web pages and so on generally use the TCP protocol.
The underlying transport of the HTTP protocol uses a reliable TCP protocol by default, but it is a big constraint on the rapid development of the Internet, and Google has developed a set of UDP-based QUIC (Quick UDP internet Co nnection) protocol, which is based between TCP and UDP, but is not yet widely used.
TCP/IP protocol is only a set of rules, and does not work, like the interface in the program, and Socket is a specific implementation of the TCP/IP protocol.
From: Han Lubiao. See spring MVC: Source Code Analysis and Practice (Web Development Technology series)
TCP/IP protocol