Linux C Advanced Programming-network programming (3)

Source: Internet
Author: User
Tags ack

Linux Network Programming (iii)--TCP


Purpose: The learning of technology is limited, the spirit of sharing is unlimited.


1.TCP segment Format

AndUDPprotocol also has the source port number and the destination port number, the communication between the two sides byIPaddress and port number identification. +bit sequence number, +confirm the Sequence number, window size. 4Head of the ministerial level andIPthe protocol header is similar, indicatingTCPthe length of the protocol header to4bytes, soTCPthe protocol header can be as long as4x15=60bytes, if there are no option fields,TCPMinimum protocol header -bytes. URG,ACK,PSH,RST,SYN,FINis a six control bit, which will be explained later in this sectionSYN,ACK,FIN,RSTfour bits, the explanations of the other bits are withheld. --bit inspection and theTCPboth the protocol header and the data are counted.

2, communication timing--"three times handshake, four wave"

The client initiates the connection, sends the request, and then the server responds to the request, and the client actively shuts down the connection. Two vertical lines indicate the two ends of the communication, from top to bottom to indicate the order of time, note that the data from one end to the other end of the network also takes time, so the arrows are oblique in the diagram. The paragraphs sent by both parties are numbered in chronological order1-10, the master in each paragraphTo mark information on an arrow, such as segment2The arrows are marked withSYN, 8000 (0), ACK 1001, <mss 1024>That represents the paragraph in theSYNlocation1, +bit sequence number is8000, the segment does not carry a payload (the number of data bytes is0),ACKlocation1, +the bit confirmation number is1001, with aMSSoption value is1024x768。

The process of establishing a connection:

1. Client Issue Segment1,SYNThe bit represents the connection request. Serial number is +, this serial number is used as a temporary ground in network traffic.Each data byte, this sequence number should be added1so that the receiver can discharge the packet correctly according to the serial numbercan also be found in the case of packet loss, in addition, the provisionSYNbits andFINThe position also takes a serial number, this time although did not send the numberBut due to theSYNbit, so the next time you send it should use the serial number1001. MSSrepresents the maximum segment size, if a segment is too large to encapsulate into frames beyond the maximum frame length of the link layer, it must beIPlayer Shard, in order to avoid this situation, the client declares its own maximum segment size, it is recommended that the server side sends the segment not to exceed this length.

The

2.  server emits a segment 2 syn ack 1001 " I received the ordinal 1000 and all previous segments, please send the next sequence number 1001 " , which is to answer the client's connection request, and also make a connection request to the client, declaring the maximum size to be 1024

3. The client sends out segment 3, responds to the server's connection request, and confirms that the serial number is 8001.

The client and the server make a connection request to each other, and also answer the connection request of the other party, where the request and response of the server is issued in a segment, so there are three segments for establishing the connection, called the " three-party handshake ( Three-wayhandshake) ". While establishing the connection, the two sides negotiated some information, such as the initial value of the serial number, the maximum segment size, etc.

In TCP Communication, if one party receives a segment from the other, reads the destination port number, and finds that no process uses the port, it will answer a segment containing the RST bit to the other party.

The process of data transfer:

1, the client emits segment 4, containing the first byte of data from the ordinal 1001 .

2 server issue segment 5 1021 , the number is 1001-1020 data indicating acknowledgement received, simultaneous request to send serial number 1021 starting data, the server also sends the client an ordinal at the same time as the reply. 8001 start 10 piggyback

3 client issue segment 6 8001-8010 data indicating acknowledgement received, request to send serial number 8011 data .

In the data transfer process, the ACK and confirm the sequence number is very important, the application to the TCP protocol sent data will be temporarily present in the TCP layer's send buffer, After sending out the packet to the other party, only the A-CK segment that received the reply will know that the packet is actually sent to the other side, and can be released from the send buffer if the packet is lost due to network failure or the ACK sent back by the other party is lost. the TCP protocol automatically re-sends packets in the send buffer after the wait time-out period .

To close the connection process:

1. The client sends out segment 7, and the FIN bit indicates a request to close the connection.

2. The server issues segment 8, answering the client's shutdown connection request.

3. The server issues segment 9, which also contains the FIN bits, which sends a close connection request to the client. 4. Client Issue Segment Ten , the answer server closes the connection request.

The process of establishing a connection is a three-party handshake, and closing the connection usually requires 4 segments, and the server's answer and close connection requests are usually incompatible and in one segment because of a semi-closed connection, in which case the client cannot send the data to the server after the connection is closed. However, the server can also send data to the client until the server closes the connection.

3. Flow control

If the sending side is sent at a faster rate, the receiver receives the data after the processing is slow, and the size of the receive buffer is fixed, the data is lost. TCP protocol through " " Sliding window ( Sliding Window) ' mechanism to solve this problem.

1.  send-side initiates the connection, declaring that the maximum segment size is 1460 0 4k " My receive buffer also has 4k bytes idle, you do not send more than the data 4k " 1024 , the initial sequence number is 8000 6k

2. Send the end of the paragraph 4-9, each segment with 1K of data, the sender of the window according to the size of the receiver to know the buffer is full, so stop sending data.

3. The receiving side of the application to lift the 2K data, the receiving buffer has 2K Idle, the receiver sends a segment , the answer has received 6K The data also declares a window size of 2K.

4. The receiving side of the application also lift 2K data, the receiving buffer has 4K free, the receiver sends out the segment one at a time, re-declared the window large and small 4K .

5. Send the end of the paragraph 12-13, each segment with 2K data, segment also contains FIN bit.

6. The receiving end responds to the received 2K data ( 6145-8192), plus the FIN bit occupies a serial number 8193 , so the answer sequence number is 8194, the connection is in a semi-closed state, and the receiving side declares the window size at 2K.

7. The receiving side of the application to lift the 2K data, the receiver to re-declare the window size of 4K.

8. The receiving side of the application to take away the remaining 2K of data, the receive buffer is empty, the receiver to re-declare the window size of 6K.

9. The receiving end of the application after the full data, decided to close the connection, the issue of the section contains The FIN bits, the sending side answer, the connection is completely closed.

At the receiving end with a small square to represent 1K data, a solid small square represents the received data, the dashed box represents the receive buffer, so the dotted box in the hollow small square represents the window size, you can see, as the application data, the dashed box is the right slide, So called sliding window.

The sending end is aKaKTo send data, and the receiving side of the application can bothKtwoKto take the data, of course, it is possible to mention it at once.3Kor6Kdata, or just a few bytes of data at a time, which means the applicationThe data you see is a whole, or a stream (Stream), the data may be split into a lot of data in the underlying communicationPacket is sent, but how many bytes of a packet are not visible to the application, soTCPa protocol is a stream-oriented protocol. andUDPis a message-oriented protocol, eachUDPsegment is a message, the application must extract the data in the message unit and cannotExtract any byte of data at a time, andTCPis very different.

Linux C Advanced Programming-network programming (3)

Related Article

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.