This paper is participating in theNetwork protocols must be known and must be known”Essay campaign

Hello, everyone, I am quick frozen fish 🐟, a river front 💦, like the colorful 💐, continuous sand sculpture 🌲, is the good brother of the cold grass of the next door. Welcome to add my wechat: Sudongyuer pull you into the group, discuss together, look forward to growing together with you 🥂.

Read this article 📖

1. You will get a full view of the web by typing the URL to the browser to display the content

2. Get a big picture view of computer networks

3. To understand the computer network from a macro perspective and build a knowledge system of computer network, Yu Yu will follow the new chapter by chapter

4. I am full of interest and hobbies in basic knowledge and cultivate my interest in computer

Preface 🌵

Learning computer network for a long time, I found that the computer network is a very interesting topic, but its knowledge broad and deep, through learning I find that many books are one part of the deep to explain, is to let a person lose interest, from the URL into the browser display picture would allow us to have a comprehensive understanding to the network from zero to one

Knowledge 📒

Create a socket

First, take a look at the internal structure of the protocol stack

The application program uses the upper abstract library Socket provided by the operating system. The Socket library is provided by the operating system, which is divided into many program components. The DNS resolution and sending data packets mentioned above are sent through the UDP module.

Common applications such as browsers and mail use TCP to send and receive data.

UDP is used for sending and receiving short control data, such as DNS query, and valid data.

The IP protocol controls the operation of sending and receiving network packets. When data is transmitted over the Internet, the data is divided into network packets, and THE IP protocol is responsible for transmitting them.

IP includes ICMP and ARP.

ICMP: Indicates whether errors occur during packet transmission and various control information.

ARP: used to query the MAC address corresponding to the IP address, generally who is XXXIP you, similar to broadcast 📢

Below the IP is the nic driver, responsible for controlling the nic hardware. The network adapter is responsible for sending and receiving data.

Protocol stack, the protocol stack is based on the control information recorded in the socket to work, there is a memory inside the protocol stack is the memory space to save control information, which records the control information of communication operations, such as IP address, port number, communication operation status.

Use netstat to view the contents of the socket

The contents of a socket

The control information is the socket entity, or the memory space of the control information is the socket entity.

The browser delegates operations through the socket library like the protocol stack.

The socket is first created, and the application calls the socket component to request the creation of the socket.

It then creates a space in memory to hold the control protocol, which is called a socket.

When you create a socket, you first allocate the memory space required by a socket and then write the initial state to it.

The descriptor in the figure is the number plate used to distinguish multiple sockets inside the computer, and the port number is the identifier used to identify the peer port number in the network

Connecting to the server

Once the socket is created, the application calls CONNECT, and the protocol stack connects the local socket to the server socket.

The connection here is actually worth the client and server exchange control information, recording some data needed to send and receive data.

There are also two types of control information:

1. Control information exchanged between the client and server. This information is required not only during connection, but also during the entire communication process, including data sending and receiving and disconnection operations.

TCP header format

2. Control information There is another type of information that is stored in sockets and used to control stack operations.

Establish a connection

Once the socket is created and the connection is established, the application calls Connect in the Soket library to begin

Connect (< descriptor >, < server IP address and port number >)

This information is handed to the TCP module, which starts exchanging the information with the SERVER’s TCP module in the following steps.

1. The client first creates the header of the control information that starts the data sending and receiving operation (mainly the sender and receiver port numbers). You can find the socket to be connected based on the sender and receiver port numbers in the TCP header.

2. After TCP is created, the TCP module will give the information to the IP module and entrust it to send out. After the IP module performs the network packet sending operation, the server IP module will deliver the packet to the TCP module after receiving the packet. Write the corresponding control information and change the status to connecting. The server TCP module then returns a response along with the SYN bit. When the response is returned, the ACK control bit is also set to 1, indicating that the network packet in response has been received. ACK is used by both parties to determine whether a network packet has been delivered. After receiving the network packet, the client writes the server IP address and port number to the socket and changes the connection status to complete. At this point, the client has completed the operation, but there is still a step to send a response packet indicating that it received the packet sent by the server, and set the ACK bit to 1. After the server receives the packet, the connection operation is complete! The socket is then ready to send receipts, like a pipe connecting the two sockets.

Sending and receiving data

Once the connection is established, you can happily start sending and receiving data by calling the write component in the Scoket library. The write component will send the data to the protocol stack. The protocol stack doesn’t care what the data is, because the data is just a sequence of binary bytes of some length.

Rather than sending data as soon as it is received, the protocol stack stores the data in an internal send buffer and waits for the next piece of data from the application. Why? Because how much data the application passes to the protocol stack at a time is controlled by the application, in order to control network transmission efficiency.

Data is sent when it has accumulated to a certain amount, and how much data is accumulated varies from operating system to operating system. But there are several factors to judge.

1. MTU(Maximum transmission unit) of data that a network packet can contain. On an Ethernet, it is usually 1500 bytes. MTU contains the total length of the header. MSS (Maximum segment size) is the maximum length of data a network packet can hold, excluding the header.

In plain English, MTU is the size of the entire network packet containing various headers (IP headers -> data)

MSS is pure data size (TCP segmentation)

2. Another factor is time. When the application to send data when the frequency is not high, if wait until every time send again closer to MSS length, waiting time would be too long, cause delayed to send, even if the buffer is not this kind of circumstance to the MSS, should also be decisive to send out, so the protocol stack inside a timer, when after a certain period of time will take the network packet to send out. (millisecond level)

⚠️ Of course, the two are contradictory. The protocol stack also gives the application flow some leeway to control the sending time. The application can specify some options to control the sending data.

Break up larger data

HTTP request messages are generally not large, but when we submit form data, the size of the data may be MSS, at this time we need to split the packet, will be split according to the LENGTH of MSS, the split data will be put into a separate network package. When these data need to be sent, each piece of data will be preceded by the TCP header, and according to the control information recorded in the socket marked with the sender port number, and then handed to the IP module to perform the operation of sending data.

Applications typically have large data, so TCP splits the data according to the size of network packets.

The packet is sent, but the data is not finished. TCP has the ability to confirm whether the network packet has been successfully received by the other party, and to resend the network packet if it has not successfully reached the target computer, because TCP still needs to confirm the operation.

Each packet is sent with a serial number and length that can be used to check whether the network packet is missing.

In fact, the serial number is not from the beginning, so it is easy to guess the attack. Remember that when we set the connection SYN to 1, we also set the sequence number field, which is the initial value of the code number.

The serial number and ACK number can be used to confirm whether the recipient has received a network packet

The sliding window

If we send a packet and wait for an ACK number, it is too wasteful. To reduce such waste, TCP uses a sliding window to manage the sending of data and the operation of ACK numbers. The so-called sliding window is that after sending a packet, it does not wait for the ACK number to return, but directly sends a series of subsequent packets. This way, the time spent waiting for the ACK number can be put to good use.

After receiving a packet, the TCP packet is stored in the receive buffer. The receiver then computs the ACK number, assembles the blocks back to the original data, and passes them to the application. If the next packet arrives before these operations are complete, don’t worry, because the next packet will also be temporarily stored in the receive buffer. If data arrives at a rate faster than the rate at which it can be processed and passed to the application, the data in the receive buffer will pile up and overflow. After the buffer overflows, subsequent packets cannot come in, and therefore the receiver cannot receive subsequent packets, which is the same as the result of an error in the middle, which means that the receiver has exceeded its processing capacity

To avoid buffer overflows, the receiver needs to tell the sender how much more data it can accept, and then the sender controls sending data based on this data. That’s the basic idea of sliding Windows.

The maximum amount of data that the receiving buffer can receive is the window size (a well-known TCP tuning parameter)

The frequent sending of packets can be reduced by combining ACK packets with the packets in the notification update window. ACK packets and the updated data in the notification update window can be sent in one packet, or multiple ACKS can be combined into the last ACK

At this point our data is sent from the client to the server, and we wait for the WEB server to return a response message. We then store the returned packets in the receiving buffer, connect the data blocks sequentially to restore the original data, and finally give the data to the application. Specifically, the protocol stack copies the received data to a memory address specified by the application, and then hands the control flow back to the application. After handing the data to the application, the stack also needs to find the right time to send window updates to the sender.

Conclusion 🍁

  • Create a socket
  • Set up the connection and set the serial number
  • Control sender data sending and receiving through sliding window
  • Store the received data in a buffer
  • Update the window size when handing buffer data to the application

References 📚

  • How the Web Is Connected
  • Diagram of TCP/IP
  • Illustrated HTTP
  • Computer Networking from top to Bottom
  • Lingo’s illustrated computer network

Conclusion 🌞

So I “from the URL to the web page 🌏” build a knowledge of computer network system – 03 ended, actually very simple, the purpose of the article is the summary of daily work and output, output some feel useful to everybody, food not food is not important, but love 🔥, hope everyone can like my articles, I am really very attentively in writing, Also hope to know more like-minded friends through this article, if you also like to toss, welcome to add my friend, sand sculpture, progress together.

Making 🤖 : sudongyu

Personal blog 👨💻: Frozen fish blog

Vx 👦 : sudongyuer

Write in the last

Guys, if you like my words, give 🐟🐟 a thumbs up 👍 or follow ➕ to support me the most.

Add me on wechat: Sudongyuer, invite you into the group and learning the front together, become a better engineer ~ (group of qr code here – > front to go to bed early, qr code has expired, see the links to the boiling point in the comments, I will put the latest qr code in the comments section, of course, can also add me WeChat I pull you into the group, after all, I also am interesting front end, I also not bad 🌟 ~