[TCP / IP] network programming: 05TCP principle - Quick description

This article main principles of TCP simple analysis and discussion.

TCP socket in the I / O buffer

The foregoing has introduced TCP traffic data borderless characteristics of the data that is sent from one end of this is not necessarily a one-time receiving end, where that remaining data in it?

In fact, write the function call does not transmit data immediately after the read function call is not immediately receive the data. Shown, write function call instant below, the data output buffer to move (when appropriate Hou transmitted to the other input buffer); Read function calls instant, data is read from the input buffer.

 TCP socket I / O buffer

 TCP's I / O buffer has the following characteristics:

  • I / O buffer present in each individual TCP socket
  • I / O buffer when generated automatically created TCP socket
  • Close the socket will continue even when the transmission data transmit buffer legacy
  • Close the socket will discard the input data in the buffer

Consider a case, the receiving end input buffer size is 50 bytes, and transmits the output side buffer size is 100 bytes, when the transmitting end of the transmission 100 bytes of data to the receiving end what happens? The receiving end buffer overflow? In fact, the receiver can properly deal with this situation, since TCP controls the data flow (Flow Control), which is a protocol of TCP sliding window (Sliding Window) mechanism ensured.

About write function calls and data exchange read (blocking mode), write timing function call is returned to the transmission side data to the output buffer; return timing of the read function is received when the buffer has data to be read.

 Internal TCP works 1: socket connected with each other

 Overall, TCP sockets disappear from creation to process the experience as follows:

  • To establish a connection with the other socket
  • Exchange data with other socket
  • Disconnect the socket connection with each other

Process to establish a connection on TCP socket as shown below, the process is the so-called three-way handshake (Three-way handshaking) mechanism.

 TCP socket connection process

[SYN] SEQ:1000,ACK:-

Established using the first connection request message field SYN (Synchronization), i.e., the synchronization message transmitting and receiving data before transmission. SEQ value 1000 indicates the packet number, ACK field is empty.

[SYN + ACK] SEQ: 2000, ACK 1001

ACK is a feedback to the end of a field, the value 1001 indicates the end of the received message number 1000.

By way of reason and confirmed packet number, you can immediately see and retransmits lost packets in the event of data loss, which is the reliable TCP messages.

About three-way handshake in host A final discussion ACK message, if the packet loss what happens?

If the final ACK message is lost, the server (host B) does not retransmit SYN + ACK message, RST packets instead sent directly into the closed state, the purpose is to prevent SYN flood attacks; another consideration, if the first client a host of SYN message ends by network congestion for a long time to get to the server, and the client is not due for a long time to establish a connection with the server and the socket is closed, the server at this time can not receive a response ACK message and, therefore, no need to retransmit SYN + ACK message.

The inner workings of TCP 2: data exchange with other host

After the three-way handshake two sides will establish a valid connection, followed by the process of data exchange, as shown below.

 TCP socket data exchange process

Three-way handshake message field and above the same communication procedure, to be noted that ACK message field value is calculated by: the number of bytes transmitted SEQ value + + 1. Plus 1 is sent to the message have been received prior to inform each other, the remaining data may be sent next. What would be lost if the packet happen?

 TCP socket data transmission error handling

As illustrated procedure, in order to complete the data packet retransmission, TCP socket starts a timer to wait for an ACK response. If the corresponding timer timeout has occurred (Time-out) is retransmitted.

The inner workings of TCP 3: Disconnect and socket connection

TCP socket end of the process is also very elegant. If the other party directly off the data to be transmitted even there will be problems, so the two sides need to negotiate when disconnected. The process is shown below, i.e. four wave (Four-way handshaking) process.

 TCP socket disconnection process

FIN packets carrying field indicates disconnected, that is, each party to disconnect after sending a FIN message.

Guess you like

Origin www.cnblogs.com/Glory-D/p/12075652.html