--TCP-depth understanding of computer network protocol

  In the near future the process of learning the computer network, because knowledge is too fragmented, trivial, so painful learning curve, but this post summarizes the details related to knowledge-based TCP transport layer protocol, and add some of my understanding, there is no innovation, understand if inappropriate, please put forward, thanks!

First had about running account, in a computer network, there are seven layer model for network transmission, or TCP / IP four-layer model and so on, then the first terms based TCP / IP four-layer model:

1. The data link layer: 0 and 1 are grouped to define a data frame, to identify the host physical address, and transfer data

2. Network layer: Define the IP address, verify the location network, forwarded by IP routing to the MAC address, the external network packet

3. Transport Layer: Define port, to confirm the identity of the host process, and the process corresponding to the data packet (using a socket)

4. Application Layer: data format is defined, and the data format according to a decoding

Based on the transport layer, there are many protocol specifies how to pass, how to collect, such as the UDP protocol, which is based on a packet (tagged added, the encapsulated data) of the transport layer protocol, but since there is no confirmation mechanism UDP first, before sending the two sides do not know whether the connection is successful, a second, once the packet is sent, can not know whether it has received, poor reliability, this time the TCP protocol was born, simply means that there is UDP protocol acknowledgment mechanism (of course, UDP protocol has many advantages, such as transmission speed, can be broadcast, multicast, etc., in particular the two complement each application, based on actual specific to project)

 

One,

TCP is a stream-oriented protocol, UDP is a message-oriented protocol data, I believe these words the teacher often said, but did not specifically explain what it means (I am anyway)

1.TCP

  We assume that there is a reservoir, you can take water and pour water in it, but the two and then pour the water is not necessarily related, then you can pour the water every five minutes then finished; then how much water, sink to less how much water, how much water, how much more will the pool water, but can not exceed the volume of the reservoir, otherwise the water will overflow, combined with TCP point of view, the pool is like the receive buffer in the kernel, pour like to send data, like water receiving the reception data, is connected to the other end you like to send data through the TCP, only one call you write, send 100 bytes, but the other may be harvested in 10 fractions, each of 10 bytes; you can also call 10 times write, each 10 bytes, but the other side can once harvested. However, the amount of data you send can not be greater than the other side of the receive buffer (flow control) , if you simply want to send excess data, then the other side of the cache is full it will put extra data discarded.

2.UDP

  Different UDP and TCP, the transmitting side several times to call write, read by the receiving terminal must read the same number of times. UPD packets is based, at the time of reception of each can only read a message, and the message packets can not be combined, if the buffer is less than the packet length, then the excess will be discarded portion . He said that if you do not specify MSG_PEEK flag, each read operation will consume a message.

 

This difference is determined by the characteristics of the TCP and UDP, TCP is a connection-oriented, that is, (connections three-way handshake, waving off four times) during the ongoing connection , the data are received in socket was issued by the same host (messages intercepted what is not considered), therefore, we knew guarantee that data is orderly arrival on the line, as to how much data each read own choice.

While UDP is a connectionless protocol, that is, as long as the receiving end knows the IP and port, and the network is reachable, any host can send a packet to the receiving end. At this time, if one can read more than one packet of data, it will be hell broke loose. For example, Host A sends the packet P1, the host B sends the packets P2, if more than one message can read the data, it will be P1 and P2 are merged with data, such data is meaningless of.


two,

Transmit and receive buffers of 1.TCP

  The transport protocol TCP end to end, one to one, then there will be sending and receiving ends, there are two operating systems in user space i.e. the space and kernal space. Each kernel Tcp socket connector has a transmit and receive buffers, TCP full duplex (the switch means is able to receive data while transmitting data, both simultaneously, which we usually call as if , speaking at the same time also be able to hear each other's voices) operating mode and TCP flow (congestion) control is dependent on two independent buffer state and filling the buffer. In programming, the two ends of a socket will send and recv by the two functions, such as Client sends data to the server, then the client is send calls to send data, but the actual effect of the Send function only copy data to transmission buffer kernel socket area, and then send the function will return, send only function is to copy the application layer buffer data is sent to the kernel buffer socket, the transmission is purely done by the TCP, and send no relationship (not to be send name to deceive a ), after the next transmits the data to the server, waits recv () is read, you can guess it, it is only the recv function to copy data from the receive buffer in the socket in the kernel buffer to the application layer only in inside, if the application has been no call to recv () to read words, this data would have been received in a corresponding socket buffer. For TCP, if the application process has not been read, after receiving buffer is full, the action is happening: the receiving end notify the originator, the receive window is closed (win = 0). This is the realization of a sliding window. That the TCP socket receive buffer does not overflow, thus ensuring a reliable TCP transport. Because the other data sent over the advertised window size does not allow. This is the TCP flow control, and if the other party ignores the window size and window size sent over the data, the receiving TCP discards it.

The flow control mechanism 2.TCP

  From the above point of view, the TCP flow control mechanism is achieved by sliding window, substantially following diagram, more details reference may be TCP / IP Comments book,

 

 ACK: indicates Reply SYN: requesting connection win: indicates the available windows

 

To be continued. . .

Guess you like

Origin www.cnblogs.com/xgmzhna/p/11505714.html