How the network requests: Chapter 1

The user inputs a URL to the browser and returns the life cycle of a network request.

The book is divided into six parts:

  1. The application layer client generates HTTP and delegates it to the protocol stack of the operating system
  2. The protocol stack (TCP/IP module) calls the network card driver to generate an electrical signal
  3. How does the network card reach the router used to access the Internet through the router
  4. Internet internal relay transmission
  5. After arriving at the web server, first pass the firewall check
  6. How does the web server collect data

The first chapter mainly introduces the main process from URL parsing to delegation protocol stack sending for client applications:

  1. The application layer client takes out the domain name or ip, port, URL, request parameters and other information according to the URL protocol format
  2. Generate request message according to HTTP protocol
  3. Before sending a message, you need to know the other party's IP and entrust the DNS client resolver to use the domain name to obtain the IP through the DNS server
  4. Use the Socket library delegation protocol stack (TCP/IP) to send requests

The main highlights are as follows:

  1. Generate HTTP message

    1. The user enters a web address (URL) into the browser. How does the browser parse the web address (URL)
    2. The browser generates an HTTP message according to the URL. What does the HTTP message look like?
  2. Obtain web server IP from DNS server

    1. How to find IP through domain name in browser
  3. Global DNS server relay

    1. How do tens of thousands of DNS relay IP queries
  4. Delegate protocol stack to send message

    1. What is the protocol stack
    2. How to delegate

Generate HTTP request information

Browser input web address (URL)

URL Uniform Resource Locator

URL format -- protocol type: / / protocol format

use HTTP Protocol access Web Server time
http://user:password@www.glasscom.com:80/dir/file1.htm
http://[user name]: [password] @ web server domain name: [port number] file pathname
​
ftp Upload and download files
ftp://user:password@ftp.glasscom.com:21/dir/file1.htm
ftp://[user name]: [password] @ ftp server domain name: [port number] file pathname
​
file://localhost/c:/path/file.zip
file://[computer name] file pathname
​
mailto:tone@glasscom.com
mailto:mailing address

Basic idea of HTTP

client ---- Request message[URI|method] ---> server
client <--- Response message[Status code]   ---- server

The request message includes what to do and what to do.

What is called a URI: uniform resource identifier, including URL

What kind of operation is called method: GET POST

HTTP protocol format

Request message
<method><Space><URI><Space><HTTP edition>
<Field name>:<field value>
<Field name>:<field value>
<Field name>:<field value>
...
<Blank line>
<Message body>
Response message
<HTTP edition><Space><Status code><Space><Response phrase>
<Field name>:<field value>
<Field name>:<field value>
<Field name>:<field value>
...
<Blank line>
<Message body>
Request header classification: Universal head(Applicable to request header and response header) Request header response header entity header
Response code | Response phrase
1xx | Inform the request processing progress and situation
2xx | success
3xx | Indicates that further action is required
4xx | Client error
5xx | Server error
Only one request can be written URI, html And pictures require separate requests
WWW working principle, The text data and the picture data embedded in the web page are stored independently|Separate request

Query the DNS server for the IP address of the Web server

In the previous step, an HTTP message is generated. You need to know the IP of the other server before sending it.

DNS: Domain Name System source code server

IP address

IP consists of network number and host number. The boundary between network number and host number is divided by subnet mask.

ip view instruction
windows ipconfig
linux ifconfig

For example:
ip: 10.11.12.13
Subnet mask: 255.255.255.0
Network number: 10.11.12
Host number: 13

32 bit, 8 bit a set

a. IP Representation of address body
10.11.12.13
IP address

b. Subnet mask expression
10.11.12.13/255.255.255.0
IP Address body/Subnet mask

c. The subnet mask is represented by the number of network number bits
10.11.12.13/24
IP Address body/Subnet mask

d. Indicates the address of the subnet
10.11.12.0/24
 All bits of the host number are 0, representing the whole subnet

d. Indicates the address of the subnet
10.11.12.255/24
 All bits of the host number are 1, which means broadcasting to the subnet

ip Address 10.11.12.255
 Subnet mask 255.255.255.0
 The subnet mask represents the boundary between the network number and the host number, The part of subnet mask 1 represents the network number, 0 Partial host number

Number of network number bits: 24 == 255.255.255.0

Domain name and IP address are used together

Human domain name, Easy to remember
 For router IP, Address length 32 bit 4 byte, High network transmission efficiency, Good performance 
note: If the server uses a virtual host, May not work IP visit

How to query IP

1. Ask about recent DNS The server, The domain name will correspond to IP
2. DNS Server return IP
There will be one on the computer DNS The client is called DNS Parser
 adopt DNS query IP It is called domain name resolution
 The parser is actually a program, Included in the operating system Socket In the library
Socket Developed by the University of California, Berkeley BSD(UNIX operating system)Medium C Language library, Other language references Socket The corresponding network library is developed
Socket A library is a collection of program components used to invoke network functions, Standard library for network development

BSD (Berkeley Software Distribution,Berkeley Software Distribution )
https://baike.baidu.com/item/BSD/3794498?fr=aladdin

Send query to DNS server through DNS resolver

<Memory address> = gethostbyname("domain name");

Parser workflow

When a network application calls the parser, the control flow of the program will be transferred to the inside of the parser.

1. Application call DNS Parser
2. DNS Parser generation DNS The server queries the message and calls the protocol stack( TCP/IP (drive)
3. Protocol stack send message
4. Send message to via network card DNS The server.

HTTP Message text, DNS Message binary

Control flow transfer: the application program calls other programs, the originally running program enters the suspended state, and the called program starts running.

Global DNS server relay

Workflow of DNS server

1. Receive query messages from clients
2. Return the response according to the message content
Query message format
a) domain name
 The server, Mail server(xxx@xxx.com @The following paragraph)name
b) Class
 Type always represents the Internet IN
c) Record type
A: The server
MX(mail eXchange): Mail server
Response message format
Record type is A  Returns the corresponding to the server IP
 Record type is MX Returns the priority and domain name corresponding to the mail server

Priority: when a mail address corresponds to multiple mail servers, the smaller the number, the higher the priority
domain nameClassRecord typeResponse data
INAxxx.xxx.xxx.xxx
glasscom.comINMX10 mail.glasscom.com
mail.glasscom.comINAxxx.xxx.xxx.xxx

Domain name hierarchy

Hierarchical saving by domain name
www.baidu.com Period separation, The more right, the higher the level

Can directly Baidu"Domain name hierarchy"

Find the appropriate DNS server and get the IP address

1. Bottom up registration
2. Top down query
register
DNS The server A(Administration baidu.com)
DNS The server B(Administration com)

A register baidu.com as well as A of ip reach B
B register com as well as B of ip To root server(.)

Root domain server IP Store in all DNS In server
 Root domain server IP There are only 13 in the world, Use multiple servers for one IP
query
The client accesses the nearest first DNS The server K, K of ip On the client side TCP/IP There are in the configuration

for example www.baidu.com

K Access root domain, return com Domain ip
K visit com field, return baidu Domain ip
K visit baidu field, return www Domain ip
K visit www field, return www.lab.glasscom.com Where ip
 last K return ip To client

DNS server cache acceleration

After querying the domain name DNS The server caches
 If the domain name does not exist, it will be cached
DNS A validity period is set for the cached information in the server. The client will be informed whether the response result comes from the cache or the server managing the domain name
Cache: the technology of storing used data in a high-speed storage device close to the data used, so as to improve the speed of subsequent access.
CPU Network cache between disk and memory

Delegate protocol stack to send message

Data sending and receiving operation

Call in order Socket Program components in the library, to OS The internal protocol stack issues a delegate.

1. Create socket (create socket phase)
2. Connect the pipe to the server socket (connection phase)
3. Manual data (communication phase)
4. Disconnecting and deleting sockets (disconnection phase) can be initiated by either client or server, usually the server

<Memory address> = gethostbyname("www.baidu.com");
<descriptor > = socket(<ipv4>, <Flow mode>);
connect(<descriptor >, <Server IP Address and port number>, ...);
write(<descriptor >, <send data>, <Send data length>);
<Received data length> = read(<descriptor >, <Receive buffer>, ...);
close(<descriptor >);

These five operations are all application delegates OS Protocol stack.
These delegate operations are performed by calling Socket Executed by program components in the library, Socket Library acts as a bridge between application and protocol stack

socket(<ipv4>, <Flow mode>); A detailed explanation of parameters is attached(https://blog.csdn.net/lee244868149/article/details/43732025)

TCP Is a streaming protocol
UDP Is a datagram mode protocol
In this paper Socket/socket/socket
Socket,Representation Library
socket,Represents the name of the program component
 Socket represents the interface at both ends of the pipeline and the abstraction of the endpoint for two-way communication between application processes on different hosts in the network. The socket connects the application process and the network protocol stack. It is the interface for the application to communicate through the network protocol and the interface for the application to interact with the network protocol stack.

Create socket

<descriptor > = socket(<ipv4>, <Flow mode|bio|nio>)

Applications and operating systems identify sockets through a set of descriptors
 Through the descriptor, the protocol stack can find the corresponding socket

Connection phase

connect(<descriptor >, <Server IP address>, <Server port number>, ...);
Descriptor: the protocol stack judges which socket to use to connect with the socket of the server according to the descriptor, and executes the connection
 The server IP: Identify server
 Server port: identifies the socket on the server

When a socket is created, the protocol stack assigns a port number (which can also be specified). Notify the server of this port number when connecting
 call connect When, perform the connection operation. After the connection is successful, the protocol will the other party IP PORT Such information is saved in the socket
Association between descriptor, port number and socket
Descriptor: the mechanism used by the application to identify the socket
IP+PORT: The mechanism of identifying each other's socket between client and server

IP Address is assigned to the network hardware installed in the device. If a device has more than one network hardware, there are more than one IP
Default port number
http 80
tomcat 8080
 Email 25
Communication phase: message delivery
write(<descriptor >, <send data>, <Send data length>);
<Received data length> = read(<descriptor >, <Receive buffer>, ...);

adopt socket call read write Delegate protocol stack to send and receive information
 The receive buffer is located inside the application. Storing messages in the receive buffer is equivalent to handing them over to the application
Disconnection phase: end of sending and receiving data
When the application receives data, the process of sending and receiving data is over. Next call Socket of close Enter the disconnection phase.
Eventually, the socket and pipe are disconnected and the socket itself is deleted.
According to different application types, which side is the client and server first close. Some application clients execute first and some servers execute first.
Disconnect process
HTTP The protocol stipulates that the server is disconnected first
web After sending the response message, the server actively performs the disconnection operation, and calls close Disconnect
web The client receives the disconnection signal and enters the disconnection phase
 Next, the browser (application) calls read Receive data, read Inform the browser (application) that the data sending and receiving operation has ended and the connection has been disconnected
 When the browser (application) learns, it calls. close Enter the disconnection phase
HTTP 1.1 Start connection multiplexing
 Actually responsible for sending and receiving messages are protocol stack, network card and driver

Keywords: Java network http

Added by barbgd on Wed, 19 Jan 2022 16:54:57 +0200