Android developers need to understand TCP / UDP network protocol

        In between the Tang Dynasty, the world is stable, because the territory is too difficult to deliver a letter, there are different persons capable of folk admonition to the emperor, each county has established Information Division to raise pigeons. It is delivered by mail pigeon. Emperor think feasible over into effect.
        Emperor day life Xiaoli from Luoyang sent a letter to the Xijing, this Xiaoli first time a letter sent to the Secretary for information, to the Information Division found that only one person handling mail. Xiaoli he gave the letters to the Information Division of the little brother, little brother saw skilful pigeon out only tell it to Xijing, on the fly, then a letter transcriptions, tore the letter three, and marked with a serial number. After a short while little brother just let go of the carrier pigeon flew back and saw little brother turn a pigeon flying. Then three pigeon out three letters respectively tied to the leg of the pigeon, and then the pigeon flying. This time Xiaoli asked the little brother sentence: "Why just only the carrier pigeon flying and hold the letter ah!." The little brother said impatiently: "I want to try, there is no nearby counties hunters we see pigeons can not fly if not fly out to find a county government troops along the line of the check.."; Small officials asked: "Now that we have seen the pigeon flew back, why put out again.". Brother continued: "Xijing over there do not know if he could send carrier pigeons I need to tell him soon..", He talked only to see the pass number 3 letter pigeons fly back. Brother said: "there are a dove flying lost.", Xiaoli quickly asked: "how to do if something goes wrong, I'm to be beheaded!.." Brother said: "Do not worry we waited a stick of incense time, if we do not come back again in the No. 2 letter sent just once.." After a stick of incense, little brother has come up with a carrier pigeon to No. 2 letter tied to the leg of a pigeon. Soon pass number 2 letter pigeon fly back. I saw little brother and come up with a skilful pigeon put out, and soon flew back to two, then little brother and put out a carrier pigeon. Xiaoli understand the question little brother: "You are doing!." Brother said: "I was over there I'll tell Xijing send information over Xijing then told me that he knew I put the letter back up and let me tell of the destruction of Atlantis no problem..." Finally the emperor's letter ran out.
        Xiaoli go back and feel more interesting, Xijing East are separated by two states, the pigeon is how how to fly it? He also made a special trip to ask the little brother of the Information Division. Brother said: "I do not know pigeons will fly to that state, if the pigeon flew to other states, other states will see the destination carrier pigeon is Xijing, he will change the direction of flight of the carrier pigeon, pigeon flew until Xijing considered the end. ".

The above story is a simple TCP transport.
Xiaoli will be the application layer, the emperor to let him go to a message.
Information Division of brother who is also transport and network layers.
Pigeon is the link layer.

 

TCP / IP layered network

1. The application layer

The application layer determines the communication activities of providing application services to users.
TCP / IP protocol suite stored within a variety of common application services. For example: HTTP, SSH, FTP and the like.

2. Transport Layer

The application layer is the lower layer of the transport layer, provided in the data transfer between two computers connected to a network
common transport layer has TCP, UDP and the like.

3. Network Layer

The network layer is also called network interconnection layer.
Network layer for processing data packets flowing on the network, the data packet is the smallest unit of data transmitted over the network. This layer defines how to reach the other side of the path by the computer, and the data packet to the other.
Role of the network layer is to choose a suitable transmission path in a number of options.

4. The link layer

Link layer, also known as the data link layer, network interface layer
handles hardware connected to the network portion, the control operating system, the hardware device driver.
We can see the switch, cable, optical fiber, also belong to the physical layer

What is TCP

TCP stands for Transmission Control Protocol (Transmission Control Protocol) transport layer is located, which has a connection protocol, providing reliable byte stream service [1]. And reliable transport service means, able to deliver accurate and reliable data to each other. Why accurate data can be served at the target, because the TCP three-way handshake protocol using a strategy, of course, in addition to three-way handshake, the TCP protocol there are other means to ensure the reliability of communication

TCP three-way handshake

① data transmission terminal first sends a packet with a SYN flag of the other

 

After return marked ② receiver receives the SYN packet with a SYN / ACK packet to confirm flag information

 

③ The sender transmits a data packet with the receiving end of the ACK flag indicates successful handshake

 More popular object-Example:

 

 

If the connection is lost after a successful handshake, also you need to re-connect again to shake hands

TCP four wave

Since the TCP connection is full-duplex, thus, each direction must be shut down separately, this principle is that when one task is finished sending data, sending a FIN to terminate the connection in this direction, but means receives a FIN There is no data on the flow in that direction, that is no longer receive data, but still be able to send data over the TCP connection until this direction also send a FIN. First off will be the one to perform active close, while the other performs a passive close.

The first wave:
Client sends a FIN, for closing the data transfer Client Server tenderer, Client enters FIN_WAIT_1 state.
Second wave:
the Server receives FIN, ACK to send a Client, the acknowledgment number for the receipt number +1 (the same SYN, FIN a occupy a sequence number), Server enters CLOSE_WAIT state.
Third Wave:
Server sends a FIN, for closing the Client Server data transfer, LAST_ACK Server enters state.
The fourth wave:
the Client receives the FIN, Client enters TIME_WAIT state, and then sends an ACK to the Server, for the receipt of the acknowledgment number number + 1, Server enters the CLOSED state, four complete wave.

 

What is UDP

UDP stands for User Datagram Protocol (User Datagram Protocol) also located in the transport layer, which is a connectionless protocol, providing a simple transaction-oriented messaging service unreliable

The difference between TCP and UDP

  1. TCP is a connection while UDP is a connectionless
  2. TCP is a reliable connection and UDP is not reliable
  3. UDP does not require a variety of reliability strategies TCP performance compared to a lot higher
  4. UDP connections because fewer steps, less so compared to the TCP header content more provincial traffic
  5. Only one TCP connection and UDP can pass messages one to one, one to many, and many-to-many

reference

References  https://blog.csdn.net/huangxy10/article/details/8012180

                 https://blog.csdn.net/qq_38872310/article/details/80220903
capture software Wireshark

Note 1: The byte stream service means, in order to facilitate transport, the bulk data is divided into segments in units of packet data services that manage

【TCP相关疑问】

   最后整理几个常见的TCP相关的疑问:

  • Q1 为什么在TCP协议里,建立连接是三次握手,而关闭连接却是四次握手呢?
  • A1 因为当处于LISTEN 状态的服务器端SOCKET当收到SYN报文(客户端希望新建一个TCP连接)后,它可以把ACK(应答作用)和SYN(同步作用)放在同一个报文里来发送给客户端。但在关闭TCP连接时,当收到对方的FIN报文时,对方仅仅表示对方没有数据发送给你了,但未必你的所有数据都已经全部发送给了对方,所以你大可不必马上关闭SOCKET(发送一个FIN报文),等你发送完剩余的数据给对方之后,再发送FIN报文给对方来表示你同意现在关闭连接了,所以通常情况下,这里的ACK报文和FIN报文都是分开发送的。
  • Q2为什么TIME_WAIT 状态还需要等2*MSL秒之后才能返回到CLOSED 状态呢?
  • A2因为虽然双方都同意关闭连接了,而且握手的4个报文也都发送完毕,按理可以直接回到CLOSED 状态(就好比从SYN_SENT 状态到ESTABLISH 状态那样),但是我们必须假想网络是不可靠的,你无法保证你最后发送的ACK报文一定会被对方收到,就是说对方处于LAST_ACK 状态下的SOCKET可能会因为超时未收到ACK报文,而重发FIN报文,所以这个TIME_WAIT 状态的作用就是用来重发可能丢失的ACK报文。
  • Q3关闭TCP连接一定需要4次挥手吗?
  • A3不一定,4次挥手关闭TCP连接是最安全的做法。但在有些时候,我们不喜欢TIME_WAIT 状态(如当MSL数值设置过大导致服务器端有太多TIME_WAIT状态的TCP连接,减少这些条目数可以更快地关闭连接,为新连接释放更多资源),这时我们可以通过设置SOCKET变量的SO_LINGER标志来避免SOCKET在close()之后进入TIME_WAIT状态,这时将通过发送RST强制终止TCP连接(取代正常的TCP四次握手的终止方式)。但这并不是一个很好的主意,TIME_WAIT 对于我们来说往往是有利的。

 

Guess you like

Origin blog.csdn.net/P876643136/article/details/90141958