At the beginning

This article is mainly aimed at the front end of the computer network this piece, focusing on the transport layer and application layer this piece of content, these content should be more important to the front end, so I feel it is necessary to understand. The article is about 8000 words, you are welcome to read! Hope to help you ~~~

From transport layer to application layer

Transport layer

What does transport layer reuse and reuse refer to?

  • Multiplexing: Processes of different applications on the sending side can use the same transport layer protocol to transfer data
  • Fragmentation: The receiving transport layer removes the header and delivers the data correctly to the application process

The process here is the application process in two communication hosts. The communication between two hosts is actually the application process in two hosts communicating with each other and exchanging data. The endpoint of communication is not the host but the process in the host.

UDP protocol (User datagram protocol –User Datagram Protocol)

UDP The unit protocol for transmitting data is UDP packets or user datagrams. It has the error detection function (detects possible data errors during data transmission and discard damaged data).

Features:

  • No connection: No connection is required before data transmission, and the transport layer of the peer party does not need to confirm the UDP packet received, reducing the cost and delay before data transmission
  • Multi-terminal transmission: support one-to-one, one-to-many, many-to-one and many-to-many interactive communication
  • Unreliable delivery: Reliable delivery is not guaranteed, but best efforts will be made to deliver
  • Packet-oriented: the system delivers a complete packet at a time. The system does not merge or split the packets from the application layer, but preserves the packet boundary.
  • No congestion control: Congestion on the network does not reduce the sending rate of the source host, so it is suitable for real-time applications such as live broadcasting and call.
  • The header is cheap: only 8 bytes, shorter than TCP’s 20-byte header

TCP (Transmission control Protocol –Transmission Control Protocol)

The unit protocol of TCP transmission is TCP packet segment

Features:

  • Connection-oriented: A TCP connection must be established before data transmission and released after data transmission, just like dialing 📞. The TCP connection is a virtual connection rather than a physical one
  • End-to-end transmission: A TCP connection can have only two endpoints
  • Reliable transmission: data transmission through TCP connection, no error, no loss, no repetition, in order to arrive.
  • Full-duplex communication: Applications on both sides of the communication can send data at any time. There are send caches and receive caches on both sides, and both sides are both sender and receiver. The send cache is used to hold data that the application sends to the sender to prepare for sending and data that TCP sends but does not receive confirmation. The receive cache is used to hold data that arrives sequentially but is not read by the receiving application and data that does not arrive sequentially.
  • Byte stream oriented: An application interacts with TCP one data block at a time (of varying sizes), but TCP treats the application as a series of unstructured byte streams. TCP has a cache, and when an application sends a block of data that is too long, TCP can cut it up and send it back. TCP does not guarantee that each block received by the receiver application is the same size as the block sent by the sender application, because various headers are added during the sending process. If an application sends only one byte at a time, TCP can also wait for enough bytes to accumulate before sending a segment.
  • The cost of the header is high: The first 20 bytes of the TCP header are fixed, and the last 4N (integer) bytes are increased as required. Therefore, the minimum length of the TCP header is 20 bytes, which is longer than the 8-byte header of UDP
How TCP reliable transmission works

Ideal transmission conditions have two characteristics: 1. Transmission channel error free; 2. 2. No matter how fast data is sent, there is always time for the receiver to process it. However, these two ideal conditions are not available in reality, so we need some reliable transport protocols.

In this case, we will use ARQ – Automatic Repeat reQuest: the retransmission reQuest is Automatic, and the receiver does not need the requester to retransmit a certain error group. ARQ includes stop waiting ARQ protocol and continuous ARQ protocol, and has error detection, positive confirmation, timeout retransmission, negative confirmation and retransmission mechanisms

  • Stop waiting protocol: After sending a packet, it stops sending and waits for the confirmation of the other party. After the confirmation, it continues to send the next packet. The advantage is that it is very simple, but the disadvantage is that the channel utilization rate is very low. Every time after sending a section of packet data, we have to wait for a long time for confirmation before continuing to send the next section of packet data, and the waiting time is wasted. Now let’s see how the stop waiting protocol works. Okay

    In the absence of error:

    If the receiver B detects an error in M1, it will discard M1 and will not notify A. If M1 is lost in the transmission process, B will not know anything at this time, so it will definitely do nothing. So how can we ensure that B can correctly receive the information sent by A?

    In fact, we can use timeout retransmission: A sets A timeout timer for each packet sent, and as long as A receives confirmation from B before the timer expires, A will cancel the timer and continue to send the next packet M2. If B’s confirmation of M1 is lost, A cannot receive the confirmation within the set time, but A does not know whether the message is wrong or lost when it is sent by itself or lost when B returns the confirmation. So we’re going to retransmit M1 after the timeout.

    Another case: there is no error in the transmission process, but B’s confirmation of M1 is late. A’s timer will time out at this time, and M1 will still be retransmitted. B will receive the duplicate M1, and then B will discard the duplicate M1 and retransmit the confirmation information.

  • Continuous ARQ protocol: that is, the sliding window protocol, is the essence of TCP. Let’s see how it works

Groups located in the sliding window can be sent continuously without waiting for each other’s confirmation, which improves channel utilization. Each time the sender receives an acknowledgement, it moves the sliding window forward by one group position, and the receiver sends an acknowledgement to the last group that arrives in sequence in a cumulative way, which means that all groups up to this group have received the acknowledgement.

TCP traffic control

In general, we want data to be transmitted quickly, but if data is sent too fast for the receiver to receive, data loss can occur. Traffic control is to ensure that the sending rate of the sender is not too fast, so that the receiver can receive data in time, and do not cause network congestion. The method of flow control is to use the sliding window mechanism.

TCP congestion control

In a certain period of time, the demand for a resource in the network exceeds the available part that the resource can provide, and the network transmission performance becomes low, which is called congestion. TCP congestion control based on sliding window method, the sender maintains a congestion window (size depends on the degree of network congestion, and dynamic change, the sender send the amount of data adjustment according to the network congestion situation), as long as the network congestion, congestion window can increase more, easy to send out more are grouped, improve the utilization of the network. However, if there is or may be congestion on the network, the congestion window should be reduced to reduce the number of packets injected into the network and alleviate the congestion on the network.

** How to determine network congestion: ** when timeout occurs, network congestion occurs.

TCP congestion control algorithm
  • Slow start: The congestion window increases gradually from small to large, and the congestion window doubles after each transmission round. There is something called slow start threshold, which is converted to congestion avoidance algorithm when the slow start increases to a certain amount.

    When congestion window < slow start threshold: use slow start algorithm

    When congestion window > slow start threshold: use congestion avoidance algorithm

    When congestion window = slow start threshold: You can use slow start or avoid congestion

  • Congestion avoidance: Let the congestion window grow slowly, increasing the congestion window by one after each transmission turn, rather than doubling it and making it grow slowly in a linear fashion

    When the network congestion (a timeout retransmission), adjust the slow start threshold = Max (congestion window / 2, 2), then put the congestion window 1, begin to execute slow start algorithm, the goal is to reduce the host quickly sent to the number of clusters in the network, let happen congested router can have enough time to deal with accumulation of grouping.

    Let’s look at a graph to compare the two algorithms

  • Fast retransmission: Enables the sender to know the loss of an individual packet segment as soon as possible. It requires the receiver to send an acknowledgement immediately rather than waiting for the data to be sent. Even if the segment is out of order, the receiver must immediately send a duplicate acknowledgement of the received packet segment. As long as the sender receives three consecutive acknowledgements, it knows that the receiver does not receive the packet segment and immediately uses fast retransmission. In this way, timeout does not occur and the sender does not mistake the network for congestion. Here’s an example

  • Fast recovery: When the sender receives three consecutive repeated acknowledgments, the sender now considers that the network may not be congested, so the slow start algorithm is not implemented, but the fast recovery algorithm is implemented, that is:

    Slow start threshold = Current congested window / 2

    New congested window = slow start threshold

    Implement the congestion avoidance algorithm to make the congestion window grow linearly

In addition, the upper limit of the send window should take the smaller of the two variables: the receiver window and the congestion window

That is, the upper limit of the sending window = Min(receiving window, congestion window), and the rate of sending data is determined by the smaller value.

Three-way handshake

The process of establishing a TCP connection is called handshake. The handshake requires the exchange of three TCP packet segments between the client and the server. Therefore, it is also called three-packet handshake.

Let’s start with a picture

Let’s start with: SYN(synchronous)- synchronous SEq (sequence)- Sequence ACK/ ACK (Acknowledgement)- Confirm FIN(Finish)- Complete

Establishing a connection is initiated by the client to the server

  • Step 1: TCP of A sends A request packet segment to B with SYN bit =1 and SEq indicates that the serial number of the first byte is X when transmitting data

  • Step 2: B receives the connection request packet segment from A and changes ACK=1 to the confirmation state with ACK= X +1.

    Choose the serial number seq=y

  • Step 3: AFTER A receives the packet, IT sends an acknowledgement to B: ACK=1. The sequence number SEq changes to SEq = X +1 based on the sequence number sent in the first step. The acknowledgement number changes to ACK= Y +1 based on the sequence number y received in the second step

Why does A need to send one connection at last? How about two connections instead?

Changing to two connections is definitely not possible. The purpose of setting up a third connection is to prevent the invalid connection request packet from being sent to B and causing an error. When the invalid connection request message is sent to B, B will give A confirmation. At this time, A will not send A final confirmation, so the connection cannot be established.

Four times to wave

After data transmission, both sides of the communication can release the connection. The process of TCP connection release is also called four-message wave

  • Step 1: Application process A sends A connection release packet segment to TCP, stops sending data, and actively closes the TCP connection. Process A makes the connection release packet segment complete state FIN=1 and sequence number SEq = U, and waits for B’s confirmation
  • Step 2: B sends an acknowledgement, the acknowledgement number is +1 in the first step, the acknowledgement number ack= U +1, and its serial number seq= V, the acknowledgement state ACK=1, the connection from A to B has been released, TCP is in the half-closed state.
  • Step 3: If B sends data, A still receives it. If B does not send data to A, the process notifies TCP to release the connection and sends the completed FIN=1, ACK=1, seq= W, and ACK= U +1 to A
  • Step 4: After receiving the connection release packet from B, A shall give corresponding confirmation. ACK=1, ACK= W +1 and seq= U +1 in its own confirmation packet segment

The application layer

The specific content of the application layer is the protocol that the application process complies with during communication. Many protocols at the application layer are based on client-server mode. The main protocols are: DNS(Domain name system), FTP(file Transfer protocol), TFTP(Simple file Transfer protocol), TELNET(remote terminal protocol), HTTP(hypertext transfer protocol – the most important), etc

Domain name System DNS

The Domain Name System is the naming System used on the Internet. The Internet uses hierarchical naming trees as host names and a distributed domain name system (DNS) to convert machine names used by people into IP addresses, which are easier to remember than IP.

DNS uses UDP as the transport layer protocol to avoid the connection delay caused by TCP and reduce the waiting time for query.

Some common top-level domain names for DNS are.cn(for China),.us(for the United States),.uk(for the United Kingdom),.com(companies and businesses),.net(web services),.org(non-profit organizations).

If you look at a spatial map of domain names on the Internet, you can see that it’s a tree structure, with domains descending from top to bottom. The corresponding domains are in the order from right to left, for examplewww.baidu.com-> com is a top-level domain name, baidu is a secondary domain name, WWW is a tertiary domain name

Domain names need to be resolved by servers all over the world. Domain name servers are hosts of the domain name system. Each domain name server has a cache of recently used names and a record of where to get the name mapping information.

classification role
Root DNS server The top-level domain name server that the local domain name server fails to resolve. All root DNS servers know the domain names and IP addresses of all top-level DNS servers.
Top-level domain name server Manages the secondary domain names registered under the TOP-LEVEL domain name server
Permission domain name server Responsible for domain name resolution of a district
Local domain name server When a host sends a DNS query request, the query is first sent to the local DNS server

Domain name resolution process:

  • The host first performs recursive query to the local DNS server
  • The local DNS server performs iterative queries to a root DNS server
  • The root server tells the local DNS server the IP address of the top-level DNS server to be queried next time
  • The local DNS server queries the TOP-LEVEL DNS server
  • The TOP-LEVEL DNS server tells the local DNS server the IP address of the rights server to query next
  • The local DNS server queries information from the permission server
  • The permission server tells the local domain name server the IP address of the host queried
  • The local DNS server finally reports the query results to the host

A complete process isClient -- -- -- -- -- -- -- -- -- -- -- -- -- - > local domain name server - > root domain name server returns -- -- -- -- > local domain name server -- -- -- -- -- -- -- -- - > top-level domain name server returns -- -- -- -- > local domain name server -- -- -- -- -- -- -- -- > the authority of the domain name server ---- return ----> local DNS server ---- return ----> Client

Recursive query

The local host sends a query request to the local DNS server and waits for the result. If the local DNS server fails to resolve the problem, it queries the DNS server as a client to other DNS servers, which is equivalent to recursing until it finds the result and returns the result IP address to the local host.

Iterative query

The local DNS system queries the root DNS server, which tells it where to go next, and it queries again, each time as a client to each server.

URL (Uniform Resource Locator)

A concise representation of the location and access of a resource available on the Internet, often called a URL

HTTP indicates the domain name of a host. The default HTTP port number is 80, which can be omitted. The path indicates /home or /about, which indicates the sub-page of a web page.

Something like this happens when the user clicks on a URL

  • Request Build – Builds the request header and the request row
  • Find the local cache
  • Prepare IP address and port -DNS resolution
  • The client initiates a request
  • The server resolves the request
  • The browser parses the response data

You can see this article in more detail what happens when the browser enters the URL through the browser display

HTTP (Hypertext Transfer Protocol)

HTTP is a protocol for sending text, pictures, video, and audio between two points on a computer network

It has some characteristics:

  • Simple, flexible, and extensible: HTTP allows the transfer of any Type of data object, which is marked by the Content-Type. The common content-type format is as follows: Text/HTML (HTML text), Text /plain(plain text), image/ JPEG (JPG image), image/ PNG (PNG image), Application/JSON (JSON data), and multipart/form-data(file upload format) )

  • Request-response mode: The client sends a request and the server responds to the request

  • Reliable transmission: Based on TCP protocol, the reliable transmission characteristics of TCP protocol are retained

  • None: Indicates that only one request can be processed per connection. The server processes the request from the client and then responds to it. The client disconnects after receiving the response, saving transmission time.

  • Stateless: Stateless means that the protocol has no memory for transaction processing. The disadvantage is that if the previous information is needed for subsequent processing, it must be retransmitted. As a result, the amount of data transmitted in each connection increases. The advantage is that if subsequent connections do not require the previously provided information, the response will be faster.

  • Plaintext transmission: This is also the disadvantage of HTTP, data is transmitted in plaintext, but it is not secure, can be monitored and pried, the transmission of information can be easily seen, and the solution is HTTPS.

  • Queue head blocking: When HTTP is enabled for long connections, the same TCP connection is shared. If a request takes too long, other requests are blocked.

The most commonly used HTTP version is HTTP1.1, which uses persistent connections by default, that is, TCP connections are not closed after, can be reused by multiple requests, can be set by the request header Connection:keep-alive, although this can save the time delay generated by each Connection. But it also puts a lot of pressure on the server.

There is also a version of HTTP2.0 (2015), which dramatically improves Web performance. Fully compatible with HTTP1.1 semantics, it has the following features:

  • Binary framing: it adds a binary framing layer between the application layer and the transport layer. At the binary framing layer, HTTP2.0 breaks all transmitted information into smaller messages and frames and encodes them in a binary format, reducing the data transfer size.
  • Header compression: HTTP1.1 does not support HTTP header compression, resulting in large data and reduced transmission speed. In HTTP2.0, we use HPACK (header compression algorithm, similar to the index table -js array, each index corresponding to a value, in transmission, is not the transmission of the corresponding header information, but the index, for the previous header information, only need to put the index (e.g. 1,2…). For example, index 2 corresponds to method method, similar to array subscript 2 corresponds to ‘GET’), using this algorithm greatly increases the transmission speed.
  • Multiplexing: Multiplexing TCP connections so that both the client and the browser can send multiple requests or responses at the same time without having to correspond in sequence. This eliminates queue congestion and greatly improves transmission performance.
  • Request priority: You can set the priority of data frames so that the server can handle important resources first to optimize the user experience.
  • Server push: The server can send multiple responses to a client request. The server also pushes resources to the client without an explicit request from the client.

HTTPS

HTTPS is the secure version of HTTP, which establishes SSL encryption layer over HTTP and encrypts transmitted data. HTTPS solves the problem that plaintext transmission in HTTP is not secure and encrypts transmitted data. See the following figure. HTTPS is an SSL layer between the application layer and the transport layer. With SSL, HTTP has the encryption, certificate, and integrity protection features of HTTPS.

The HTTPS protocol relies on TLS/SSL, and their functionality depends on TLS/SSL

  • Symmetric encryption: use the same key for encryption and decryption. A password cannot be decrypted without a key; conversely, anyone with a key can decrypt it. In this way, it is difficult to ensure that the key can be safely forwarded in the process of transmission, and it is also difficult to keep after receiving it. Anyway, it is easy to fall into the hands of attackers, so it is only 8 too far to use this way.
  • Asymmetric encryption: It uses both public and private keys. The server stores the private key and distributes the public key to the client. The server uses one pair of private keys to communicate with multiple pairs of public keys. However, it takes time to encrypt and decrypt data, which reduces transmission efficiency.

HTTPS uses a combination of these two encryption methods

The advantage of using symmetric keys is that the decryption efficiency is fast. The advantage of using asymmetric keys is that the transmitted content cannot be cracked. Even if the data transmitted by the client is intercepted, it cannot be cracked without the private key of the server. HTTPS uses symmetric encryption for content transmission. Asymmetric encryption only applies to certificate verification.

The specific steps are:

  • The server sends its public key (Key1) to the ca to apply for a certificate.
  • Certificate authorities also have a pair of public and private keys of their own. The organization uses its own private key to encrypt Key1 and generates a certificate signature from information such as the server url, which is also encrypted by the organization’s private key. After the certificate is created, the organization sends the certificate to the server.
  • When the client requests the server for communication, the server does not directly return its public key (Key1), but returns the certificate applied for by itself to the client.
  • The first thing the client needs to do after receiving the certificate is to verify its authenticity. It is important to note that browsers and operating systems already maintain the names and public keys of all authority certificates. Therefore, the client only needs to know which institution issues the certificate, and can find the corresponding public key locally and decrypt the certificate signature. After the certificate is verified successfully, the client can safely use the public key of the organization again to decrypt the public key Key1 of the server. The certificate is the digital certificate in HTTPS, the certificate number is the digital signature, and the third party is the exponential certificate Issuing Authority (CA).
  • The client randomly generates its symmetric key Key2, encrypts it with the server public key Key1, and sends it to the server.
  • The server unlocks the encryption with its own private key to obtain the symmetric encryption key Key2. The client and server then begin symmetrically encrypted communication using Key2.

The general process is: Server application certificate is sent to the client, the client according to decrypt the server public key certificate, to get the server’s public key correctly, and then the client randomly generated a symmetric encryption key, using the public key for encryption and transmission access for the server, the server through decrypt again get the symmetric key, following all the information through the symmetric key to encrypt decrypt, Complete the HTTPS process.

Differences between HTTP and HTTPS

  • HTTP plaintext transmission, HTTPS ciphertext transmission

  • HTTPS is more secure than HTTP, friendlier to search engines, and better for SEO. Google and Baidu index HTTPS pages first.

  • HTTPS requires an SSL certificate, but HTTP does not.

  • HTTPS standard port 443, HTTP standard port 80;

  • HTTPS is based on the transport layer and HTTP is based on the application layer.

👉 concludes with a brief summary of Ajax

Encapsulating an Ajax

Here are a few things you can learn about Ajax

  • readyState

    • 0 – (uninitialized) The send() method has not been called
    • 1 – (load) The send() method has been called and the request is being sent
    • 2 – (load completed) The send() method is complete and all responses have been received
    • 3 – (Interaction) Parsing the response content
    • 4 – (Done) The response content is parsed and can now be called on the client side (main)
  • Status (HTTP status code)

    • 1XX: message that the server receives the request and requires the requester to continue the operation (uncommon)
    • 2XX: Succeeded. The operation is received and processed successfully. Common: 200-OK (normal return), 201-created (successfully Created), 204-no Content (server does not return Content when the page is not updated)
    • 3XX: redirect, further action is required to complete the request. Common: 301-Moved Permanently: Permanently redirected Permanently, the requested resource has been Permanently Moved to a new URI, and the returned information contains the new URI. The browser will automatically redirect to the new URI. The client should continue to use the original URI, 304-not Modified. The requested resource is unmodified and no resource is returned. Clients usually cache accessed resources.
    • 4XX: Client error status, indicating that the server cannot process the request. Common: 400-Bad Request (the client Request syntax is incorrect and the server cannot understand it), 401-UNAUTHORIZED (The client Request is not authorized), 403-forbidden (the server understands the Request but refuses to execute it. 404 – Not Found
    • 5XX: Server error. An error occurred while processing the request. Common: 500 (server internal error, unable to complete requests), 503 (overloaded or in maintenance, temporarily unable to process requests)

Wrapped code

function getJson(options) {
  // This options is a configuration parameter passed to Ajax
  return new Promise((resolve, reject) = > {
    // Return a promise object, resolve on success, reject on failure
    if(! options.url) {// The path to request
      console.log("Please enter url")
      return
    }
    let method = options.method || "GET" // If there is no request mode, the default is get
    let async = options.async || true // Whether Ajax requests the default bit true asynchronously
    let xhr = new XMLHttpRequest()
    if (method === "GET") {
      xhr.open(method, options.url, async)
      xhr.send()
    } else if (method === "POST") {
      xhr.open(method, options.url, async)
      xhr.setRequestHeader("Content-Type"."application/x-www-form-urlencoded");
      xhr.send(options.data)
    }
    xhr.onreadystatechange = () = > {
      if (xhr.readyState === 4 && xhr.responseText) {
        // Data indicates success
        resolve(xhr.responseText)
      }
    }
    xhr.onerror = err= > {
      reject(err)
    }
  })
}

/ / call API
getJson({
  url: "".method: "",
}).then(res= > {
  console.log(res)
})
Copy the code

conclusion

The article temporarily wrote this first, because oneself this semester just learned the computer network, the teaching material that we use in class also is Xie Xiren compiled “computer network”, in addition to this, also collected all kinds of information about the network on the Internet, collected down. In view of some of the contents of their own network knowledge for a simple summary, but also for their future job to lay some foundation. Most of the content in this article should be front-end need to master, so focus on writing these, like the physical layer in the network, link layer, network layer these what is not summarized, want to know can also go to see. If there is something wrong in the content, you are welcome to criticize! Those who like me or student party is also preparing to find a job, I hope we come on together! Mutual encouragement!! Code word is not easy, the handsome men and women passing by move their fingers in this cold winter movement movement point a thumbs-up 8, ball ball you!

The articles

Summary of the front end questions

reference

Computer Networks – Xie Xiren

Reinforce your HTTP body of knowledge