Hello, everyone
I am the public number: JAVA small hero to refueling.


Today I would like to share a knowledge point about computer networks
How is the Internet connected?

  • Don’t say a word, just drive

The browser generates the message and sends it

  • The overall flow of sending a message is as follows

Generate the HTTP request message

For chestnut, when we enter the browser https://www.jdl.cn/img/servic… Internet address

  • The browser first parses the URL

    • HTTPS: Represents the mechanism for accessing the data source, namely the protocol
    • www.jdl.cn: Web server name
    • Img: Represents the directory name
    • Service.843585b7.png: Represents the file name

And then you’re going to generate an HTTP message, which looks something like this



What are the specific contents of these fields for reference in this articleFive thousand words small composition, yes, we are have an HTTP.

DNS domain names resolve to IP addresses

After the browser generates the HTTP message, where does it send it? Of course is the server, so it is necessary to analyze the domain name corresponding to which server, IP address is what, because the IP address is not easy to remember, so there is the corresponding domain name, for our human memory.

  1. The browser checks the cache for an IP address corresponding to the domain name
  2. The operating system checks the cache (also known as the hosts file)
  3. The operating system will send it to the DNS server in the region and ask it to parse it for you

The DNS server accepts queries from the client, including the following three items

  • Domain name: server, name of mail server
  • Class: When DNS was first designed, its use on other networks than the Internet was taken into account. Class was used to identify network information, but today there is no other network, so the value of Class always represents the IN of the Internet
  • Record type: Indicates what record type the domain name corresponds to

    • A, the domain name directly corresponds to the IP address
    • When CNAME, this domain name corresponds to another domain name
    • Mx indicates that the domain name corresponds to the mail server

    The response data is different for different record types

The hierarchy of domain names

  • The further to the right, the higher the level, the level from right to left: for example, www.jdl.cn is CN-> JDL -> WWW
  • Domain information with this hierarchy is registered with the DNS server, and each domain is handled as a whole

The interaction between the client and the DNS server is as follows

  • The parent DNS server should register the IP address of its subordinate domain, and then the parent DNS server should register the IP address of the higher level DNS server, and so on
  • The root DNS server information is stored in all the DNS servers in the Internet, so that all the DNS servers will find the root domain, and then search down until they find the domain they want
  • There are only 13 IP addresses assigned to the root domain, which are the IP addresses corresponding to the top-level domain names (com,cn, etc.)



The interaction looks something like this



However, one server can’t store so many, so it is usually the DNS server relay to find the IP address, as shown in the figure below

The client finds the nearest DNS server and looks for the information of www.jdl.cn. However, the nearest DNS server does not have this information, so it forwards it to the root domain server. After judgment, it finds that it is the top-level domain name of CN, so the root domain DNS server will return the IP address of the DNS server in the CN domain it manages. The nearest DNS server goes back to the COM domain server and so on, eventually finding the IP address of the server www.jdl.cn

The delegate stack sends messages

Once you know the IP address, you can delegate to the operating system’s internal protocol stack to send a message to the target IP address

  • The internal structure of the protocol stack

  • General applications, such as browsers and mail, use TCP to send and receive data
  • DNS queries such as sending and receiving shorter control data using UDP

The network layer

  • OSI seven-layer model

Open System Interconnection and Communication Reference Model Open System Interconnection Reference Model, abbreviated as OSI, is a conceptual Model proposed by the International Organization for Standardization (ISO). It is a standard framework that attempts to make all kinds of computers in the world interconnect as a network. It is defined in ISO/IEC 7498-1.

  • TCP/IP quaternary model

    • Application layer: HTTP, DNS, FTP
    • Transport layer: TCP, UDP
    • Network layer: IP
    • Network interface layer

TCP/IP refers not only to the two protocols, TCP and IP. But refers to a protocol cluster composed of FTP, SMTP, TCP, UDP, IP and other protocols, just because in the TCP/IP protocol TCP protocol and IP protocol is the most representative, so it is called TCP/IP protocol

The client server passes the data flow

  • A packet has to be processed at every level from the client to the server
  • The client side needs to constantly add headers to the packet
  • The server side needs to constantly split the packet

Three-way handshake

When two computers want to transfer data, they must connect first, through TCP three handshakes, we usually say that TCP connection through three handshakes, let’s take a look at what exactly is TCP three handshakes, as shown in the picture

  • When the client wants to send it, it will open it actively from Closed state, and the server has been in the listening state since it started
  • The client sends SYN = 1,seq = x to the server, and the client is in SYN_SEND state.
  • SYN =1, ACK =1, seq = y, ACK = x+1 The server is in the SYN_RCVD state at this point
  • When the client receives it, it sends ACK =1, seq = x+1, ACK = y+1 to the server, and the state of the client is estab-lished
  • When the server receives it, the state becomes estab-lished
  • After three handshakes, the client and server can pass packets to each other
  • So we’re talking about SYN, ACK,seq, ACK what are those? These are actually properties in the TCP packet, which we’ll look at next (explained in the transport layer).

The application layer

HTTP packet splitting

  • HTTP request messages are generally not too long, a network package can be installed
  • If the data in the send buffer exceeds the MSS length, it will be split MSS length into separate network packets
  • MTU (Maximum Transmission Unit) : The Maximum length of a network packet, typically 1500 bytes in Ethernet
  • Maximum Segment Size (MSS) : The Maximum length of TCP data that a network packet can hold, excluding headers

The transport layer

  • Then the network packet in the upper application layer is added to the TCP header

TCP Message Format

  • Source port number (16 bits) : The port number from which the network packet is sent
  • Destination port number (16 bits) : The port number of the receiver of the network packet
  • Serial number (the sequence number in which data is sent) (32 bits) : The byte at which the sender tells the receiver that all data has been received
  • Acknowledgement number (the sequential number of data received) (32 bits) : The byte by which the receiver tells the sender that the receiver has received all data
  • Heading length (4 bits) : Indicates the beginning of the data, the offset of the data
  • Reserved (6 bits) : This field is reserved and is not currently used
  • Control bit (6 bits) : Each bit in this field represents the meaning of the following communication control

    • URG: Indicates that the emergency pointer field is valid
    • ACK: indicates that the serial number field of the received data is valid. Generally, it means that the data has been received by the receiver
    • PSH: Represents data sent by FLUSH operation
    • RST: Forced disconnection, used in case of abnormal interruption
    • SYN: The sender and receiver mutually confirm the serial number, indicating the connection operation
    • FIN: Indicates disconnection operation
  • Window size (16 bits) : The receiver tells the sender the window size (that is, data that can be sent together without waiting for confirmation)
  • Checksum (16 bits) : Used to check for errors
  • Emergency pointer (16 bits) : Indicates the location of data for emergency processing
  • Optional fields (variable length) : Optional fields can be added in addition to the fixed header fields above, but they are rarely used except for join operations

Remember the various serial numbers that were mentioned in the three handshakes, the properties of this message

The network layer

  • Then the top network packet plus the IP header

IP message format

  • Version number (4 bits) : IP protocol version number, currently version 4
  • Header Length (4 bits) : The length of the IP header. Optional fields can cause the length of the header to vary, so the length of the header needs to be specified here
  • Service type (TOS) (8 bits) : Represents packet transport priority. The definition of this parameter was vague in the original protocol specification, and more recently the Diffserv rule has redefined the use of this field
  • Total length (16 bits) : Represents the total length of the IP message
  • ID number (16 bits) : The number used to identify the packet, usually the serial number. If a packet is IP sharded, all shards have the same ID
  • Flag (3 bits) : This field has three bits, two of which are valid, representing whether sharding is allowed and whether the current sharding package is a sharding package
  • Shard Offset (13 bits) : Indicates the content of the current packet starting at the first byte of the entire IP message
  • Lifetime (TTL) (8 bits) : Represents the lifetime of a packet. This is to prevent a packet from spinning in the network forever if the network loops back. With each router that passes, the value is reduced by one, and if it drops to zero, the hi packet is discarded
  • Protocol number (8 bits) : The protocol number indicates the type of protocol (all the following are hexadecimal)

    • TCP: 06
    • UDP: 17
    • ICMP: 01
  • Header checksum (16 bits) : used to check for errors. No longer in use
  • Sender IP address (32 bits) : The IP address of the sender of the network packet
  • Receiver IP address (32 bits) : The IP address of the receiver of the network packet
  • Optional fields (variable length) : Optional fields can be added in addition to the fixed header fields above, but they are rarely used except for join operations
  • Then the network packet is added to the Mac header

MAC packets

  • Receiver MAC address (48 bits) : The MAC address of the receiver of the network packet, which is used to transmit the network packet in the LAN
  • Sender MAC address (48 bits) : The MAC address of the sender of a network packet by which the receiver can determine who sent the network packet
  • Etheric type (16 bits) : The type of protocol used. The following are some common types that are typically used only in TCP/IP communication: 0800 and 0806.

    • The 0000-05 dc: IEEE 802.3
    • 0800: IP protocol
    • 0806: ARP protocol
    • 86DD : IPV6

MAC address vs. IP address

  • The IP header is also preceded by the MAC header
  • Why do you need MAC packets? Because in the Ethernet world, the TCP/IP idea just doesn’t work.
  • Ethernet determines the destination of a network packet in a different way than TCP/IP does, so a packet must be sent to the destination in a way that matches it. MAC addresses do just that
  • Sender MAC address: MAC address is written in the network card production into the ROM, just need to read this value out and write the MA header is good

The sender’s MAC address is relatively easy to get, but the receiver’s MAC address is less easy to get

ARP broadcast

  • ARP :Addresss Resolution Protocal address Resolution protocol
  • ARP broadcasting is used when looking up the receiver’s MAC address based on the IP address
  • In the same subnet, use broadcast to ask all devices who is the IP address XXX, if other devices find their own IP address is this XXX, then they will tell its MAC address to the questioner, so that it will detect the receiver’s MAC address, if they find their own IP address is not this XXX, The message is then discarded and ignored.

  • If we broadcast every time, we would add a lot of ARP packets to the network, so for efficiency, we would have ARP cached in memory. Query the ARP cache before querying it.
  • If the destination’s IP address corresponds to a different MAC address, the MAC cache will fail, so to avoid this problem, the cache will be deleted after a few minutes, very simple and crudely.

    • Static ARP: Manual maintenance, not automatic failure
    • Dynamic ARP: automatically invalidates after a period of time.
  • The IP module is responsible for adding the following two headers:

    • MAC header: The header used for Ethernet, containing the MAC address
    • IP header: The header used for IP, containing the IP address

Global packet

The packet at this point looks like this

  • MTU (Maximum Transmission Unit) : The Maximum length of a network packet, typically 1500 bytes in Ethernet
  • Maximum Segment Size (MSS) : The Maximum length of TCP data that a network packet can hold, excluding headers
  • Then the packet, along the network card, travels to hubs and routers (involving electrical signal conversion, etc.), to the server, where it is peeled off layer by layer (as I said before).

disconnect

Four times to wave

After the two computers are finally connected, disconnect the connection and wave four times

Actually,
Three-way handshake.
Four times to waveThere are so many more things to talk about, like why you need to shake hands three times and wave hands four times. I’ll talk to you about this separately in the future, so be sure to watch