1、The essence of Internet is a series of network protocols.
A hard-wired operating system, then loaded with software you can use normally, but you can only use it yourself.
Like this, everyone has a machine of their own, but they are isolated from each other.
English has become the universal standard of communication for all people in the world. If you look at computers as people all over the world, the Internet that connects two computers actually means
A series of unified standards, these standards are called Internet Protocol, the essence of the Internet is a series of protocols, called Internet Protocol Suite.
Functions of Internet Protocol: Standards that define how a computer accesses the Internet and computer communications to the Internet.
1.1osiSeven layer protocol
The internet protocol is divided into OSI seven layers or tcp/ip five layers or tcp/ip four layers according to their functions.
Physical devices running on each level
1.2、tcp/ipFive layer model explanation
We will use application layer, presentation layer, session layer and application layer to explain the origin and function of each layer from the perspective of TCP / IP five-layer protocol, and make clear the main protocol of each layer.
It understands the whole principle of Internet communication.
First of all, what the user perceives is only the top layer of the application layer, each layer from top to bottom depends on the next layer, so we start from the bottom layer, it is better to understand.
Each layer runs a specific protocol, the closer it gets closer to the user, the closer it is to the hardware.
1.2.1、physical layer
Origin of the Physical Layer: As mentioned above, isolated computers must be connected to the Internet if they want to play together. The implication is that computers must be networked.
Physical layer functions: mainly based on electrical characteristics to send high and low voltage (electrical signals), high voltage corresponding to digital 1, low voltage corresponding to digital 0
2.3.2 data link layer
Origin of the Data Link Layer: Simple electrical signals 0 and 1 are meaningless and must specify how many bits a group of electrical signals each means
The function of data link layer: the grouping method of electrical signals is defined.
Ethernet protocol:
In the early days, companies had their own way of grouping, and then a unified standard, ethernet, emerged.
ethernetRegulations
- A set of electrical signals form a packet called frame.
- Each data frame is divided into two parts: header head and data data.
head | data |
headContains: (fixed 18 bytes)
- Sender / source address, 6 bytes
- Receiver / target address, 6 bytes.
- Data type, 6 bytes
dataContains: (the shortest 46 bytes, the longest 1500 bytes).
- Contents of data packets
headLength + data length = shortest 64 bytes, longest 1518 bytes.
macAddress:
headThe source and destination address contained in:ethernetAll devices that require access to Internet must have NIC.,The address of the sending end and the receiving end refers to the address of the network card, that is, the MAC address.
macAddress: Each card is fired to a world-unique MAC address, 48-bit binary in length, usually denoted by a 12-bit hexadecimal number (the first six are vendor numbers, the last six are pipeline numbers).

Binary:Using voltage to transmit dataHigh voltage stands for 1 low voltage represents 0Decimal system:128 64 32 16 8 4 2 1 Binary: 00000000Number of digits:8 7 6 5 4 3 2 1 The binary of character 2 in the ASCII table.0011 0010 Decimal 50 sixteen hexadecimal 32In the ASCII table, the binary of characters!0010 0001 Decimal 33 sixteen hexadecimal 212 If the decimal 50 - > binary 110010 ! If the decimal system is 33 ->Binary 100001 If the computer needs to send characters 2! Conversion to electrical signal is 110010.100001 The computer does not recognize the space, so the real sending signal is 110010100001 high and low voltage transmission.At this point, if a computer needs to recognize such information, it is necessary to define a set of rules artificially. For example, each 8 signal represents a character 001100100010.0001Ord can see the decimal representation of corresponding characters in Python.>>> ord("!") 33 >>> C:\Users\Administrator>ipconfig /all Windows IP To configureHost name,...: USER-20171226LK The main DNS suffix.Node type.IP routing is.......No: noThe WINS agent has been enabled.Ethernet adapter local connection:Connect specific DNS suffix.Description..........: Realtek PCIe GBE Family ControllerA.... E9-D5-5E-03-7A-E9 ####MAC address of NIC DHCP It has been.Automatic configuration is...Local link IPv6 address......: fe80:: ad1d:5018:876b:3d00%12(First choice)IPv4 address:...192.168.0.106(First choice)....255.255.255.0 The time of obtaining the lease.9:22:04 The expiration date of the lease.15:22:04 The default.192.168.0.1 DHCP The server.192.168.0.1 DHCPv6 IAID . . . . . . . . . . . : 266392926 DHCPv6 Client DUID.00-01-00-01-21-D3-6D-11-E0-D5-5E-03-6A-B5 DNS The server.218.2.2.2
View Code
Radio broadcast:
With a Mac address, two hosts in the same network can communicate (one host gets the MAC address of the other through the ARP protocol)
ethernetIn the most primitive way, the way of broadcasting is to communicate, that is to say, computer communication basically relies on roar.
2.3.3 network layer
Origin of the Network Layer: With ethernet, MAC addresses, broadcasting, the world’s computers can communicate with each other. The problem is that the world’s Internet is made up of
If all communications are broadcast over Ethernet, packets sent by one machine will be received worldwide.
This is not only a matter of low efficiency, it will be a disaster.
The above conclusion: A way must be found to distinguish which computers belong to the same broadcast domain from which computers are not, and if they are sent by broadcast, if not by broadcast.
In the case of routing (distributing packets to different broadcast domains / subnetworks), MAC addresses are indistinguishable and are only vendor related
Network Layer Function: Introduce a new set of addresses to distinguish different broadcast domains/subnets, which are network addresses
IPAgreement:
- The protocol that specifies the network address is called the IP protocol, which defines the address as the IP address. The widely used version of V4 is IP v4, which specifies that the network address is represented by a 32-bit binary system
- Scope 0.0.0.0-255.255.255.255
- A IP address is usually written in four decimal numbers. Example: 172.16.10.1
ipAddress is divided into two parts.
- Network part: identification subnet
- Host part: identification host
Note: Simple IP address segments identify only the types of IP addresses, and neither the network nor the host can identify a subnet in which the IP is located.
Example: 172.16.10.1 and 172.16.10.2 can not be sure that the two are in the same subnet.
Subnet mask
The so-called “subnet mask” is a parameter representing the characteristics of the subnet. It is formally equivalent to the IP address, is also a 32-bit binary number, its network part is all 1, the host part is all 0. For example, the IP address 172.16.10.1, if the known network part is the first 2.Four bits, the host part is the last eight bits, then the subnet mask is 11111111.11111111.11111111111.11111111.00000000, written in decimal is 255.255.255.0.
Knowing the “subnet mask” allows us to determine whether any two IP addresses are on the same subnet. The method is to perform an AND operation on two IP addresses and a subnet mask (both digits are 1, the result is 1, otherwise it is 0), and then compare the results if the same, if so, on the table.They are in the same sub network, otherwise they are not.
For example, subnet masks with known IP addresses 172.16.10.1 and 172.16.10.2 are 255.255.255.0. Are they on the same subnet? The AND and the subnet mask are calculated respectively.
172.16.10.1:10101100.00010000.00001010.000000001
255255.255.255.0:11111111.11111111.11111111.00000000
ANDNet address result: 10101100.00010000.00001010.000000001-> 172.16.10.0
172.16.10.2:10101100.00010000.00001010.000000010
255255.255.255.0:11111111.11111111.11111111.00000000
ANDNet address result: 10101100.00010000.00001010.000000001-> 172.16.10.0
The result is 172.16.10.0, so they are in the same subnet.
To sum up, the IP protocol has two main functions, one is to assign IP addresses to each computer, the other is to determine which addresses are in the same subnetwork.
ipdata packet
ipPackets are also divided into header and data sections, which are placed directly into the data section of the Ethernet packet without defining a separate column for the IP packet
head:The length is 20 to 60 bytes.
data:The longest is 65515 bytes.
The Ethernet data packet’s “data” part is only 1500 bytes long. Therefore, if the IP packet exceeds 1500 bytes, it needs to be split into several Ethernet packets and sent separately.
Ethernet header | ip Head | ipData |
ARPAgreement
arpProtocol Origin: Computer communications basically rely on roaring, that is, broadcast mode, all the upper layer of the packet to the end of the package to encapsulate the Ethernet head, and then sent through the Ethernet protocol, when talking about the Ethernet protocol, we learned that
Communication is based on MAC broadcasting mode. it is easy for a computer to obtain its own MAC when sending packets. how to obtain the MAC of the target host requires the adoption of ARP protocol.
arpProtocol function: broadcast the way to send packets, get the MAC address of the target host.
Protocol work: every host IP is known.
For example, host 172.16.10.10/24 access 172.16.10.11/24
One: first, distinguish the sub net from the IP address and the subnet mask area.
scene | Packet address |
The same subnet | Target host Mac, target host IP |
Different subnets | Gateway Mac, target host IP |
2: Analyze that 172.16.10.10/24 and 172.16.10.11/24 are on the same network (if not the same network, the target IP in the following table is 172.16.10.1, and the gateway MAC is obtained through arp)
Source MAC | Target mac | Source IP | Target IP | Data section | |
Sending host | Sending end mac | FF:FF:FF:FF:FF:FF | 172.16.10.10/24 | 172.16.10.11/24 | data |
3: This packet will be broadcast in the sender’s own network transmission, all hosts after receiving the packet, found that the target IP for their own, then respond, return to their own MAC

network layerThe IPv4 version provides that:192.168.0.106 IPv4 address11000000.10101000.00000000.01101010 255.255.255.0 Subnet mask11111111.11111111.11111111.00000000 Calculate the hidden network address through AND operation.11000000.10101000.00000000.01101010 IPv4 address11111111.11111111.11111111.00000000 Subnet mask11000000.10101000.00000000.00000000 network address192.168.0.0 After calculating, it is found that all the computers in LAN are the same. In general, all electronic devices connected by routers in the family form a small LAN.Another representation of a subnet mask; because the subnet mask is fixed, 192 can be denoted in decimal 24 after the IP address.168.0.106 /24 Representing 192.168.0.106 /255.255.255.0 data link layer.....head...|.......data.......... macsource| macTarget Data sectionnetwork layerEthernet header...|......ip Head............ipData...Ethernet header...|Source IP, target ip+ mask........ipData...Mac source| macTarget source IP, target ip+ mask,.....Ip data...
View Code
2.3.4 transport layer
The origin of the transport layer: the IP of the network layer helps us to distinguish the subnet, the MAC of the Ethernet layer helps us to find the host, and then everyone uses the application, your computer may open qq, storm video, and many other applications at the same time.
So we found a specific host through the IP and mac, how to identify the application on this host, the answer is the port, the port is the application and the network card associated with the number.
Transport layer function: establishing port to port communication
Supplement: port range 0-65535, 0-1023 for system occupancy port.
tcpAgreement:
Reliable transmission, TCP packets have no length limit, theoretically can be unlimited, but in order to ensure network efficiency, usually the length of TCP packets will not exceed the length of IP packets, to ensure that a single TCP packet does not need to be split.
Ethernet head | ip Head | tcpHead | Data |
udpAgreement:
Unreliable transmission, “header” section has a total of only 8 bytes, the total length does not exceed 65,535 bytes, just put into an IP packet.
Ethernet head | ipHead | udpHead | Data |
tcpmessage
tcpThe three handshake and the four wave.
2.3.5 application layer
Origin of Application Layer: Users use all the applications, all work in the application layer, the Internet is developed, everyone can develop their own applications, a variety of data, we must specify the form of data organization.
Application layer function: Specifies the data format of application.
Example: TCP protocol can transmit data for various programs, such as Email, WWW, FTP and so on. There must be different protocols that specify the formats of e-mail, Web pages, and FTP data, and these application protocols form the “application layer”.
2.3.6 socket
We know that if two processes need to communicate the most basic premise may be able to uniquely mark a process, in local process communication we can use PID to uniquely mark a process, but PID is only locally unique, the network of two processes PID conflict probability is very high, this time we needInstead, we know that the IP address of the IP layer can uniquely identify the host, while the TCP layer protocol and port number can uniquely identify a process of the host, so we can uniquely identify a process in the network using the IP address + protocol + port number.
After they can uniquely identify processes in the network, they can communicate using sockets. What is a socket? We often translate sockets into sockets, which are an abstraction layer between the application layer and the transport layer that manipulates the complex operations of the TCP / IP layerAbstraction provides interfaces for several simple interfaces to call the implemented processes in the network.
socketOriginated from UNIX, socket is an implementation of “open-read/write-close” mode under the Unix philosophy that everything is a file. The server and the client maintain a “file” separately. After the connection is opened, they can write to their files for each other to read or read.Content, closing the file at the end of the communication.
Three. Realization of network communication
To achieve network communication, each host needs four elements.
- The IP address of this machine
- Subnet mask
- IP address of gateway
- DNSIP address
There are two ways to get these four elements.
1.Static acquisition
Manual configuration
2.Dynamic acquisition
Get through DHCP
Ethernet head | iphead | udphead | dhcpdata packet |
(1)The front “Ethernet header” sets the MAC address of the sender (local) and the MAC address of the receiver (DHCP server). The former is the MAC address of the local network card, the latter does not know at this time, fill in a broadcast address: FF-FF-FF-FF-FF.
(2)The “IP header” behind it sets the IP address of the sending party and the IP address of the recipient. At this point, neither of these two machines is known. Therefore, the IP address of the sending party is set to 0.0.0.0, and the recipient’s IP address is set to 255.255.255.255.
(3)The last “UDP header” sets the port of the sending party and the port of the recipient. This part is stipulated in the DHCP agreement, the transmitter is the 68 port and the receiver is the 67 port.
After the completion of the packet, it can be issued. Ethernet is broadcast, and every computer in the same sub network receives this package. Because the MAC address of the receiver is FF-FF-FF-FF-FF-FF-FF, it can’t tell to whom it was sent, so every computer that receives this packet,It is also necessary to analyze the IP address of the package before deciding whether to send it to it. When you see the sender’s IP address is 0.0.0.0 and the receiver’s 255.255.255.255, the DHCP server knows “This package is sent to me” and other computers can discard it.A bag.
Next, the DHCP server reads out the packet’s data content, assigns the IP address, and sends back a “DHCP response” packet. The structure of the response packet is similar. The MAC address of the Ethernet header is the network card address of both sides, and the IP address of the IP header is the DHCP server’s.IP addresses (sender) and 255.255.255.255.255 (receiver), UDP header ports are 67 (sender) and 68 (receiver), the IP addresses assigned to the requester and the specific parameters of the network are included in the Data section.
The newly added computer receives the response packet and knows its IP address, subnet mask, gateway address, DNS server, and so on.
Four. Network communication process
1.Local access
- The IP address of this machine is 192.168.1.100
- Subnet mask: 255.255.255.0
- IP address of gateway: 192.168.1.1
- DNSIP address: 8.8.8.8
2.Open the browser, want to visit Google, and enter the address in the address bar:www.google.com。
3.dnsProtocol (based on UDP protocol)
13Table root DNS:
A.root-servers.net198.41.0.4U.S.A
B.root-servers.net192.228.79.201USA (another support IPv6)
C.root-servers.net192.33.4.12France
D.root-servers.net128.8.10.90U.S.A
E.root-servers.net192.203.230.10U.S.A
F.root-servers.net192.5.5.241USA (another support IPv6)
G.root-servers.net192.112.36.4U.S.A
H.root-servers.net128.63.2.53USA (another support IPv6)
I.root-servers.net192.36.148.17Sweden
J.root-servers.net192.58.128.30U.S.A
K.root-servers.net193.0.14.129UK (another support IPv6)
L.root-servers.net198.32.64.12U.S.A
M.root-servers.net202.12.27.33Japan (another support for IPv6)
Domain name definition: http://jingyan.baidu.com/article/1974b289a649daf4b1f774cb.html
Top-level domain names: to. com,. Net,. Org,. CN and so on belong to the international top-level domain names, according to the current Internet domain name system, international top-level domain names are divided into two categories: category top-level domain names (gTLD) and geographical top-level domain names (ccTLD). Category top-level domain name isDomain names ending with “COM”, “NET”, “ORG”, “BIZ” and “INFO” are managed by foreign companies. The geographic top-level domain name is the domain name ending with country or region code. For example, “CN” represents China, “U”.K “represents Britain. Geographic top-level domains are usually managed by various countries or regions.
Second-level domain name: The second-level domain name is a geographical domain name based on the top-level domain name, metaphorical China’s second-level domain has,.com.cn,.net.cn,.org.cn,.gd.cn and so on.W.abc.com or *.abc.com.
Generally speaking, a secondary domain name is a record of a domain name. For example, alidiedie.com is a domain name, and www.alidiedie.com is a relatively common record. This is usually used by default, but the domain names like *.alidiedie.com are all.Called the two level of alidiedie.com.
4.HTTPPart of the content is similar to the following:
GET / HTTP/1.1
Host: www.google.com
Connection: keep-alive
User-Agent: Mozilla/5.0 (Windows NT 6.1) ……
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding: gzip,deflate,sdch
Accept-Language: zh-CN,zh;q=0.8
Accept-Charset: GBK,utf-8;q=0.7,*;q=0.3
Cookie: … …
We assume that the length of this part is 4960 bytes, and it will be embedded in the TCP packet.
5 TCPAgreement
TCPPackets need to be set to ports, the HTTP port of the receiver (Google) is 80 by default, and the port of the sender (native) is a randomly generated integer between 1024 and 65535, assuming 51775.
TCPThe header length of the packet is 20 bytes, plus the packet embedded in HTTP, the total length is 4980 bytes.
6 IPAgreement
The TCP packets are then embedded into the IP packets. IP packets require IP addresses for both parties, known as 192.168.1.100 for the sender and 172.194.72.105 for the receiver.
IPThe header length of the packet is 20 bytes, plus the embedded TCP packet, the total length is 5000 bytes.
7 Ethernet protocol
Finally, IP packets are embedded into Ethernet packets. Ethernet packets need to set the MAC addresses of both sides, the sender is the MAC address of the local network card, and the receiver is the MAC address of gateway 192.168.1.1 (obtained by ARP protocol).
The maximum length of the data portion of the Ethernet packet is 1500 bytes, whereas the current IP packet length is 5000 bytes. Therefore, IP packets must be partitioned into four packets. Because each packet has its own IP header (20 bytes), the IP packets of the four packets are 15 in length, respectively.00, 1500, 1500, 560.
8 Server side response
Google’s server, 172.194.72.105, received the four Ethernet packets after forwarding from multiple gateways.
According to the serial number of the IP header, Google combines the four packets, takes out the complete TCP packet, reads out the “HTTP request” in it, makes the “HTTP response” and sends it back with the TCP protocol.
After receiving the HTTP response, the machine can display the web page and complete a network communication.

Binary:Using voltage to transmit dataHigh voltage stands for 1 low voltage represents 0Decimal system:128 64 32 16 8 4 2 1 Binary: 00000000Number of digits:8 7 6 5 4 3 2 1 The binary of character 2 in the ASCII table.0011 0010 Decimal 50 sixteen hexadecimal 32In the ASCII table, the binary of characters!0010 0001 Decimal 33 sixteen hexadecimal 212 If the decimal 50 - > binary 110010 ! If the decimal system is 33 ->Binary 100001 If the computer needs to send characters 2! Conversion to electrical signal is 110010.100001 The computer does not recognize the space, so the real sending signal is 110010100001 high and low voltage transmission.At this point, if a computer needs to recognize such information, it is necessary to define a set of rules artificially. For example, each 8 signal represents a character 001100100010.0001Ord can see the decimal representation of corresponding characters in Python.>>> ord("!") 33 >>> C:\Users\Administrator>ipconfig /all Windows IP To configureHost name,...: USER-20171226LK The main DNS suffix.Node type.IP routing is.......No: noThe WINS agent has been enabled.Ethernet adapter local connection:Connect specific DNS suffix.Description..........: Realtek PCIe GBE Family ControllerA.... E9-D5-5E-03-7A-E9 ####MAC address of NIC DHCP It has been.Automatic configuration is...Local link IPv6 address......: fe80:: ad1d:5018:876b:3d00%12(First choice)IPv4 address:...192.168.0.106(First choice)....255.255.255.0 The time of obtaining the lease.9:22:04 The expiration date of the lease.15:22:04 The default.192.168.0.1 DHCP The server.192.168.0.1 DHCPv6 IAID . . . . . . . . . . . : 266392926 DHCPv6 Client DUID.00-01-00-01-21-D3-6D-11-E0-D5-5E-03-6A-B5 DNS The server.218.2.2.2 network layerThe IPv4 version provides that:192.168.0.106 IPv4 address11000000.10101000.00000000.01101010 255.255.255.0 Subnet mask11111111.11111111.11111111.00000000 Calculate the hidden network address through AND operation.11000000.10101000.00000000.01101010 IPv4 address11111111.11111111.11111111.00000000 Subnet mask11000000.10101000.00000000.00000000 network address192.168.0.0 After calculating, it is found that all the computers in LAN are the same. In general, all electronic devices connected by routers in the family form a small LAN.Another representation of a subnet mask; because the subnet mask is fixed, 192 can be denoted in decimal 24 after the IP address.168.0.106 /24 Representing 192.168.0.106 /255.255.255.0 data link layer.....head...|.......data.......... macsource| macTarget Data sectionnetwork layerEthernet header...|......ip Head............ipData...Ethernet header...|Source IP, target ip+ mask........ipData...Mac source| macTarget source IP, target ip+ mask.........ipData...address resolution protocolPackage:
View Code
DHCPThe service is integrated on the router at home.
Switchboard
The two level switch is based on MAC address propagation and integrated in the data link layer.
The connection between the three layer switch servers is integrated with the function of routing.
The four layer switch transmission layer is based on port forwarding.