Between their own network knowledge rotten in a complete mess, so ready to write the relevant network article, but consider all written in one is too long, so separate write, I hope you can look carefully, the best can point out my mistakes, let me also can correct.

1. Explain the whole network architecture:

Android Skill Tree – Network Summary (1) network Architecture

TCP, UDP,socket,websocket, HTTP, HTTPS. What is webService? It is similar to websocket. Socket and Websocket are similar in terms of session,token and cookie.

Android Skill Tree – Network Summary (2) TCP/UDP

Android Skill Tree – Web Summary (3) HTTP/HTTPS

Android skill tree – network summary (4) of the socket/websocket/webservice

Cookie /session/token (to be written)

3. Source code analysis of relevant third party frameworks, after all, okHTTP and Retrofit source code is a must for interviewing large companies now.

Android Skill Tree – Network summary (6) OkHttp super super super super super super super super super super detail analysis

Android Skill Tree – Network Summary (7) Retrofit source code detailed parsing


The body of the

In the network architecture summary we know the general network architecture hierarchy, this article focuses on some of the usual we hear familiar words TCP and UDP.

TCP and UDP:

In the network architecture summary we mentioned TCP/IP’s four network tiers:

TCP (Transmission Control Protocol) and UPD (User Datagram Protocol) are at the transport layer, so we know that UDP and TCP are used to transmit data. To provide communication between different processes in the host, since it is the transfer of data, let’s use the logic of express delivery and mail delivery as an example (anyway, something gets from one place to another).

TCP is like express delivery, because now we can see where the specific express has arrived, whether a transfer station has received my express, whether the final destination has received my express, and if the express is lost, we will give you feedback and help you to make up for it. UDP is more like sending a letter. I send it anyway, and I don’t care whether I receive it or not. (emmm…. Maybe this is not the right example…)

So our preliminary feeling: compared with UDP, TCP transmission is reliable, error-free.

1.1 Connection and Disconnection of TCP Channels

Since the data is going from one place to another, we need to establish a channel so that the data can flow later. (PS: This analogy may not be appropriate. If you have a better metaphor, leave a comment. Thank you)

TCP three times handshake, four times wave is not a lot, yes, this is used to set up the channel and break the channel, when I interview people, three times set up, four times break basically know, but I ask them why three times, a lot of people can’t answer. For better memory, let’s use concrete examples:

Three handshakes:

1. A messaged B: Are you there? It's urgent!!!!! 2. B sends A message to A: I'm here. Is it urgent? Then tell me. I'm listening to you all the time.Copy the code

Unfortunately, A had diarrhea at this time, so she had to run to the toilet immediately. Then she had to pull for half an hour, and THEN B had to wait for half an hour.

At this time, do you find the problem of the second handshake? If A does not immediately reply the corresponding message to B after B sends it to A for the second time, B can think that A is no longer there, so it will not wait for it or establish A channel.

So it should go something like this:

1. A messaged B: Are you there? It's urgent!!!!! 2. B sends A message to A: I'm here. Is it urgent? Then tell me. I'm listening to you all the time. 3. A sends A message to B: Here's the thing. I want you to hear me out. balabala....... balabala....... balabala.......Copy the code

Then the channel between A and B is open, and A can send messages to B continuously at this time.

And then some people say, well, TCP doesn’t have diarrhea, so why does TCP have three, because if you have two: A sent A message to B to apply for establishing A channel, but B did not receive it due to the network delay. At that time, A was impatient and withdrew directly. However, AFTER A while, B received this message and thought that A had just sent A request, so IT established A channel, but A was already gone. This prevents B from forming deadlocks, wasting resources, and so on.

Of course, here is our example, which must be passed by some values:

Four waves:

We know that we can send messages to each other after the TCP connection. Here we assume that the channel actually contains two small channels, one for A to B and one for B to A, so that when we want to disconnect, we have two steps:

  1. Disconnect the channel from A to B
  2. Disconnect the channel through which B sends messages to A

Let’s start by disconnecting the channel from A to B:

A messaged B: I'm tired. I'll go to bed first.Copy the code

At this time, A will go to sleep, and A will not send messages to B. But at this time, B can still continue to send messages to A, B may suddenly make A deep confession late at night

B sends A message to A: ACTUALLY I XXXXXXXX.Copy the code

Therefore, it is not enough for us to simply wave twice, but also to disconnect the channel from B to A:

B sent A message to A: But you said you were going to bed, I think it's late, I'm going to bed too, good night. A messaged B: Then you go to bed earlier, too. Good nightCopy the code

So together they are:

A messaged B: I'm tired, I'm going to bed first, 88. B messaged A: Ok, you go to bed first, B messaged A: But you said you were going to bed, I think it's late, I'm going to bed too, good night. A messaged B: Then you go to bed earlier, too. Good nightCopy the code

The actual four waves must also be a value notification. The diagram looks like this:

At first it was two-way communication, and then after the second wave, the A to B connection was broken, so it became one-way data transmission, and then the second wave shut down the one-way data transmission.

So we see TCP connection and disconnect are so many steps, multiple confirmation and other operations, but UDP does not need to establish a stable channel, directly send the data over. So UDP is faster because you don’t have to establish a connection first.

1.2 TCP Error-free transmission

We usually certainly heard the TCP transmission security, UDP transmission insecurity, TCP transmission to ensure that the data can finally stable and safe to arrive at the destination, and UDP only sent in the past, not responsible for the final received, the specific reason is why??

I still to download the tool “Thunderbolt” to illustrate (may thunderbolt function to achieve more complex, I simply used to illustrate the TCP example, if the example is not right, welcome to point out) :

Question 1: Is the thunderbolt download TCP or UDP?

We must have experienced that 99% of the downloaded content is downloaded, and maybe the file is useless, indicating that we have received 100% of the downloaded files during the transfer. This is definitely TCP to control, because UDP sent after whether you receive it or not.

Problem 2: When using it to download things, suddenly the network was poor for a period of time, at that time, the server could not receive the package sent, but finally a complete package was downloaded (similar to the feeling of continuing to download the thunderbolt).

In fact, I would like to elaborate on this issue: for example, if a document is divided into 100 copies and I receive the third copy due to poor network, how can it verify that I have not received the third copy? Instead of being hot-headed, it directly sends the fourth and fifth copies to me. I guess I didn’t get the third one, so I got a third one. And if you control it instead of sending all 100 at once, you send some, the receiver gets some, and then sends some more.

As said above, there are 100 copies, but the receiver does not receive the third copy. In this case, the sender should not continue to send the fourth and fifth copies, indicating that the receiver has given feedback to the sender, as follows:

A needs to send 100 documents to B through QQ, but these documents need to be accepted in order. In this case, A sends A file, B accepts A file, and then the interface will have the corresponding prompt:Copy the code

At this time, A knows that B has received two files and starts to send the third one. If B does not receive a message indicating that the file was successfully received after a certain period of time, B continues to send a 3.txt file. For A, if QQ does not see the prompt that 3. TXT is accepted, but directly receives the request that 4. TXT is accepted, you must ignore it directly, instead of downloading the 4. TXT file or waiting for the 3. TXT file again. In any case, if B does not receive the message that A has successfully downloaded the 3. TXT file, it will resend 3. TXT.Copy the code

The sender:

For the sender: Each received a confirmation frames, send window sliding forward a frame of distance When the window is sent to send frames (i.e., inside the window frame is sent but not yet received confirmation of the frame), the sender will stop to send, until received the receiver send confirmation frames to Windows mobile, Windows have can send frames, then began to continue to send Specific as follows:

The receiver:

For the receiving end: after receiving the data frame, move the window forward one position and send back the confirmation frame. If the received data frame falls outside the receiving window, it will be discarded.

Important features of the sliding window protocol:

  • The send window can only slide forward if the receive window slides forward and the receiver sends the confirmation frame (only if the sender receives the confirmation frame)
  • The stop-wait protocol, the back N-frame protocol and the select retransmission protocol differ only in the size of the send window and the receive window:

1. Stop waiting protocol: send window size =1, receive window size =1; That is, a single frame sliding window is equal to stop-wait protocol 2. Back N frame protocol: send window size >1, receive window size =1. 3. Select the retransmission protocol: the size of the sending window is >1, and the size of the receiving window is >1.

  • When the size of the receive window is 1, the order of frames can be guaranteed.
  • In the sliding window protocol of the data link layer, the size of the window is fixed during transmission (note to be different from the sliding window protocol of TCP).

1.3 Differences between TCP and UDP

The Angle TCP UCP
Whether connection Connection-oriented (connections need to be established before data can be sent) No connection (Data is sent without connection)
Whether to retry packet loss Various control functions of data transmission can be realized, including retransmission control of lost packets and sequential control of out-of-order subcontracting Packet loss retries are not performed and the order of arrival is not corrected
model Stream mode (byte stream oriented) Datagram mode (message-oriented)
Corresponding relations between One to one Supports one-to-one, one-to-many, many-to-one and many-to-many interactive communication
The head of overhead Minimum 20 bytes It’s only 8 bytes
reliability Full duplex is very reliable, error-free, not lost, not duplicated, and arrives in sequence No guarantee of reliable delivery, no guarantee of sequential arrival
Congestion control A controlled Congestion control, so congestion on the network does not slow down the transmission rate of the source host (useful for real-time applications, such as IP telephony, real-time video conferencing, etc.)
Resource requirements TCP program structure is more complex, more UDP procedure structure is simple, less

Conclusion:

In fact, there are a lot of THINGS you can say about TCP, such as packet segment format, congestion control, and so on. This article is more about more knowledge points that we usually hear, combined with easy-to-understand explanation. Of course, I have a very poor Internet, so I welcome you to point out some of my mistakes.

Computer Networking: this is a comprehensive & detailed TCP protocol walkthrough, some of the pictures are introduced in this article.

Reference article:

Computer networking: this is a comprehensive & detailed TCP protocol walkthrough