Fifth Transport layer (UDP and TCP three-time handshake, four wave waves analysis)

Source: Internet
Author: User

Preface

Through this chapter, can know in fact three times handshake and four waves actually really good simple, through this chapter of learning, I believe you will also think, in the future do not need to hear others ask three times the process of shaking hands and oneself a face Meng forced, think someone good dick, in fact, he knows you do not understand, only this, do not understand to learn. Learn you will feel that in fact, there is no serious, this let me recall the previous learning programming, when just learned C, others said will write Java HelloWorld, really TM think Dick Ah, I even helloworld is what degree do not know. have been envious of others, how so strong, and then their own heart is very empty, so that their own food ah, actually, do not understand to learn, learn to understand you do not feel anything great, so say, not compared with others, but to exceed their own. Hope to help the students now confused.

The focus is on TCP's three-time handshake and four waves, as well as some features of TCP.

--wh

First, Transport Layer overview

The functionality of this layer is also quite simple, the Transport layer provides the application layer to provide end-to-end communication services , popular speaking, two host communications, that is, the application layer of communication between processes, that is, the transition to process and process communication between, we have learned the network layer, IP protocol can send packets to the destination host accurately, but stay in the network layer, do not know how to hand over to our host application process, through the previous study, we learn to have a MAC address, through the MAC address can find the same network under the host, IP address, through the IP address can find the network under different networks, Combined with the MAC address to find the corresponding host, then how to find the host application process, there must be a thing to identify it, that is what we often say the port .

Port, occupies 16 bits, its size also has 65,536, is from 0~65535. That is, a computer has 65,535 ports, the communication between the host, that is, the communication between the application process, the degree depends on the port, a process corresponding to a port, process A and process B communication, Process A to the port is 60000, process B to the port is 60001, process A through port 60000 send data to process B, we know to give 60001 port, also to process B, so as to achieve the purpose of communication.

      Well-known ports, enlistment ports, client ports

      Well- known port : 0-1023, that is, some fixed port number, such as the 80 port used by HTTP, which means that when the URL is visited, we access the server port is 80, and then the server over the page to send us the data.

      Registration Port : 1024-49151, such as Microsoft developed a system application, the application in the communication or use, the use of the xxx port, then go to register this port, in order to avoid the other company's application use the same port number, for example, Windows system 3389 port, is used to implement remote connection, fixed this computer if you want to use the Remote Connection service, open 3389 port, others can use remote connection you, the default is not open.

      Client Port : 49152-65535, generally we use a certain software, such as QQ, and other services, randomly take this range of ports, rather than to get the front of which fixed, get the communication end, will release the port.

Know what the port is? What did the transport layer specifically do? Transport layer is to connect two ports to communicate media, otherwise the light knows what two ports are used, how to communicate, or to rely on transport layer to do this thing, which is important is to rely on two protocol, UDP and TCP protocol. The specific contents of the UDP and TCP protocols are given below.

Second, UDP protocol

Udp:user Datagram Protocol User Datagram Protocol.

No connection, unreliable

No connection: means that you do not need to establish a connection before the communication, transfer data directly.

Unreliable: A packet of datagrams is sent from one host to another, but the datagram is not guaranteed to reach the other end, and any required reliability is provided by the application. In the case of UDP, although you can ensure that the message size is sent, there is no guarantee that the message will reach the destination. There is no timeout and retransmission function, when the UDP data is encapsulated in the IP datagram transmission, if lost, an ICMP error message is sent to the source host. Even if there is a network congestion situation, UDP is unable to control traffic. Additionally, UDP is not responsible for retransmission even in the event of a packet loss, even when the packet arrives in a disorderly order without correcting the function

       

Header format for UDP

              

      Look at the UDP header first,

SOURCE port number: 16 bits, port number used by the source host's application process

Target port number: 16 Bits, the port number used by the target host's application process, which is the target process we need to communicate with

UDP datagram Length: The length of the UDP user datagram, the sum of the data part +UDP header is the UDP datagram length.

Inspection and: Testing and is designed to provide a reliable UDP header and data, not to be confused with the unreliable transmission above, here to provide a reliable UDP header, because a process may accept a number of processes over the message, then how to distinguish them, that is, by 5 things to differentiate, "source IP Address "," Destination IP Address "," protocol Number "," Source port number "," Target port number " , this detection is reliable, is to detect which of the correct message, that is, which message to enter this port, that is unreliable, said that the message may be lost, which may be corrupted data we do not care , but the premise is that you have to transfer to the right destination, or disorderly out of the hair data report, it is not a mess.

      UDP pseudo Header

is to get some data from the IP layer, because the data must be available for inspection and verification. The algorithm tested in the IP layer is the same as the test header method.

Analysis

          

A target process in which the message, destination port, destination IP address must be the same as the city, but the source IP address and the source port may be different, this means that the source and the same destination of the message will be located to the same queue. This is not the same as the TCP we are going to discuss, because UDP is not connected, we use this channel, so in its queue there will be the above mentioned situation.

Examples of using UDP protocol

1, the Application layer protocol DNS, that is, according to the Domain name resolution IP address of a protocol, he is using UDP

2, DHCP, this is to assign IP address to each computer protocol, which is also used UDP protocol

3, IGMP, we say that multicast, that is, the use of UDP, the multimedia teacher, the teacher took the notebook lectures, we in the following through their computer to see the teacher's picture, this is the transmission of data through UDP, so there will be some classmate card, some students are fluent, because it can not be relied on transmission, but card a bit, There's no image of the next watch.

There are a lot of degrees to use the UDP protocol.

Third, the TCP protocol

TCP protocol is a kind of protocol for connection-oriented, reliable transmission, flow control, congestion control, and character stream-oriented transmission. Its final function, like UDP, communicates between end and end, but the difference from UDP is still very large.

      Structure of TCP messages

      

1. Source Port number

2. Target port number

3, serial number: Because in TCP is the byte stream, he will divide the message into bytes, to each byte sequence number, such as a message has 900 bytes, then will be compiled into 1-900 serial numbers, and then divided into several points for transmission, such as the first pass, the serial number is 1, passed 50 bytes , then the second pass, the serial number is 51, so the serial number is the first byte of the transmitted data relative to all the byte position.

4, confirm the answer: As the example just said, the first time to pass 50 bytes to each other, the other side will respond to you, which with a confirmation answer, is to tell you the next time to pass the 51st byte, so this confirmation answer is to tell each other how many bytes

5, the first length: is the length of the first,

6, Reservations: To be used in the future, the position of this reservation is similar to the control bit

7, control bit: The current control bit is 6

URG: Emergency, when the URG is 1 o'clock, the table name emergency pointer field is valid, the identification of the message is an emergency message, sent to the target host, do not queue, should let the message as far as possible, let it get the application to accept.

ACK: Confirm that the serial number is valid when the ACK is 1 o'clock. When the ACK is 0 o'clock, confirm that the serial number is useless

PSH: Push, when it is 1 o'clock, when encountering this message, will reduce the data up delivery, would like to apply process delivery data is to wait until a certain size of the cache to send, but encountered it, do not have to wait for enough data to be delivered, but let the application process to get this message earlier, this need and emergency division clear, emergency is queue , but the data that commits the cache size does not change, the push is queued, but when it encounters him, it reduces the amount of cached data delivered and delivers it in advance.

RST: RESET, the message encountered a serious error, such as TCP connection error, and so on, will be the RST set to 1, and then release the connection, all again.

SYN: Synchronous, when the connection is made, that is, three times the handshake is used, the following will be specific, with the use of ACK together

FIN: Terminates when the connection is released, that is, four times when the wave is used.

8. Window: Refers to the size of the receiving window of the sending message segment, which controls the amount of data sent by the other party (starting from the confirmation number, allowing the other party to send the data). That is, the window size of the sliding window you need to talk about later

9, Inspection and: test header and data of the two parts, like UDP, need to get the data in the pseudo-header to help detect

10, Option: variable length, introduce an option, the maximum message segment length, MSS. To be able to tell the other side of TCP, the maximum length of a data field that my cache can accept is the MSS bytes. If no option is used, then the header is fixed at 20 bytes.

11. Fill: To make it a whole number of bytes

      Connection oriented

(three-time handshake): Before communication, a three-time handshake mechanism is used to confirm that the connection between the two ports is available. And UDP does not need to confirm whether the availability, direct transmission.

Three-time handshake mechanism.

                

At the beginning of the client and server City shutdown state, but at some point, the client needs to communicate with the service side, both sides will be prepared port, the port of the segment will be listening state, waiting for the client's connection. The client can know its own port number, and the port number of the destination process, so that the request can be initiated.

First handshake: The client wants to connect with the server, so the state becomes active open, sending a connection request message to the server segment syn=1, and carries x bytes past. After sending the request connection message, the state of the client becomes syn_sent, it can be said that this state is waiting to send the acknowledgement (in order to send a third handshake confirmation package)

Second handshake: After the server receives the connection request message, it changes from the Lstten state to the passive open state, then returns a message to the client. This message has two layers of meaning, one is to confirm the message, and can reach to tell the client, I also opened the connection. After sending, it becomes SYN_RCVD state (it can also be said to wait to accept the confirmation status, accept the client sent the confirmation package)

Third handshake: The client gets the server-side confirmation and know that the server side is ready to connect, but also send a confirmation message to the server side, tell the server side, I received the message you sent, then let us two to connect. After the client sends the confirmation message, it enters the established, and the server is received and becomes established

After entering the established state, the connection is complete and communication is possible.

Question: Why do you need a third handshake, two times before?

Assuming there is no third handshake, the client sends a connection request message in the past, but because of the network delay, after waiting for a timeout, the client will resend a request connection message in the past, and then proceed normally, the server sends back a confirmation connection message, and then begins the communication, after the communication is over, The first time because of the network delay request connection message to the server side, the server does not know that the message has been invalidated, but also sent back a confirmation connection message, the client received, found that he did not send a connection request (because time-out, so that they did not send), so to this confirm connection request do not do anything , but at this point the client does not think so, he thinks I connection has been established, has been open to wait for the client to transmit data, which caused a great waste. If you have a third handshake, the client can notify the server. So the third handshake is also very important.

      Open connection request at the same time

Under normal circumstances, the communication party requests to establish a connection, the other party should request, but if it occurs, both sides of the communication request to establish a connection, the connection establishment process is not three handshake process, and this situation is only one connection, and does not establish two connections. At the same time, when the connection is opened, both sides send syn almost simultaneously and enter the Syn_sent state, when each end receives the SYN, the state becomes SYN_RCVD, and both sides send SYN and ACK as the acknowledgment reply to the received Syn. When both sides receive the SYN and the corresponding ACK, the state becomes established

            

      Reliable transmission

Through 1, data number and accumulation confirmation 2, in bytes of sliding window 3, timeout retransmission time 4, rapid retransmission of these four aspects to achieve the purpose of reliable transmission.

1. Data number: Numbering each byte, 900 bytes, numbering from 1 to 900

1, accumulate confirmation: The server side is not receive a byte to send a confirmation, that efficiency is too low, but when received 4, 5, in sending a confirmation, then before the confirmation of the data even if sent successfully.

2. Sliding window: This is the same as a sliding window in the data link layer. Each time the data that can be sent is in this window, how much data is received, how much data will be slid backwards

3, timeout retransmission time: This also in the link layer said, if waiting for a period of time, has not received the confirmation message, then re-transmit

4, Rapid retransmission: In the sliding window application, such as the transfer of 1234 6 to the server side, the old-fashioned is after 4 of all the data to be re-transmitted, and this fast retransmission only need to wait to pass the serial number 5, you can continue to receive data down.

         

      Flow control

In the transport layer, there are accept cache and send cache two things exist, so each time the data sent past the other end, will take the data to the past, let the other side know the size of their two caches, and then reasonably set their own size of the sending window, if the other side of the cache is almost full, The other side in the transmission of data, they will tell themselves, less a little bit of data, they set a small sliding window, so that the other side has the opportunity to buffer, without causing the cache overflow, do not let their messages are discarded.

      Congestion control

In fact, with the flow control, but the angle of the station is larger, at this time not only to consider the other side to receive, cache too much overflow caused, and consider in the line, the transmission rate on the line is so big, but there are many people at the same time, send too much data, it will make the line found congestion, that is, the router These two issues result in a large amount of data loss. So congestion control This solution, probably means that when the detection of network congestion, it will make their own sliding window smaller, but how the specific change, is based on the algorithm to forget,

Upper value of the send window = Min[rwnd,cwnd]

Rwnd: Accept the window, according to accept the cache, and accept the window, receive cache There are many, then the receiving window is large

CWnd: Congestion window, according to the congestion situation in the line to decide, the line is not congested, then this window is large,

The Send window is taken with a small value of two. This is still understandable.

Slow start algorithm, fast recovery algorithm, combined to achieve congestion control, want to understand the two algorithms, Baidu Baidu Wikipedia

      Four waves when TCP releases a connection

Once the communication is complete, the connection is released and the four-time wave mechanism is performed to complete the task. (Draw to draw or think the official figure is good.) )

        

First wave: From established to active shutdown state, the client actively sends release connection request to server side, Fin=1. After sending, it becomes the fin_wait_1 state, which can be said to wait for the confirmation state.

Second wave: The server receives a request from the client to release the connection, the status changes to Close_wait, and then sends a confirmation message to the client, telling him that I received your request. Why becomes close_wait, because the client sends the release connection request, may own this side also has the data not to send to finish, so this time the entire TCP connection state becomes the semi-shutdown state. The server can also send data, and the client can also receive data, but the client can not send the data, only to send a confirmation message. When the client receives a confirmation message from the server, it enters the fin_wait_2

State. It can also be said that this is waiting for the server to release the connection status.

The third wave: the server side of all the data sent out, think can close the connection, the state becomes passive shutdown, so send the release of the connection message to the client, after sending itself into a last_wait state, that is, waiting for the client to confirm the state

Fourth wave: After the client has released the connection message, send a confirmation message, and then turn itself into a time_wait, instead of shutting down immediately, because the client sends a confirmation message may be lost, lost the server will be re-transmit a fin, that is, release the connection message, this time the client must not shut down. When the server receives the acknowledgement message, the server enters the close state, which is closed. But for this reason, the client must wait for a certain amount of time before it can enter the close state.

  

The above is the four wave mechanism that the TCP connection releases. Very simple.

        Close the connection at the same time

Normally, the communication side requests that the connection is closed, the other party responds to the connection shutdown request, and the connection is closed passively. However, if the connection request is closed at the same time, both sides of the communication are transitioning from the established state to the fin_wait_1 state. When either party receives the FIN segment from the other side, its status is changed from Fin_wait_1 to CLOSING state, and the last ACK data segment is sent. When the last ACK data segment is received, the state transitions to time_wait, after waiting for 2MSL time to enter the CLOSED state, eventually releasing the entire TCP transport connection. Its process into the next

                  

As an example of using TCP, it is generally necessary to ensure that the data is reliable, and the degree uses the TCP protocol.

1, the HTTP protocol for Web site access, the use of TCP.

          

Fifth Transport layer (UDP and TCP three-time handshake, four wave waves analysis)

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.