1. What Is a Computer Network?
1.1 Definition of a Computer Network
A computer network is a system in which multiple devices with independent computing capabilities are connected through communication links and network protocols to achieve data exchange, resource sharing, and collaborative work.
These devices can be:
- Desktop computers and laptops.
- Mobile phones and tablets.
- Servers and cloud computing instances.
- Routers, switches, and firewalls.
- Cameras, sensors, and smart home devices.
- Industrial control devices and embedded terminals.
The core of a computer network is not simply connecting devices, but establishing a complete communication mechanism that allows different devices to exchange data according to common rules.
For example, when a user visits a website in a browser:
- The browser prepares to access the target server.
- The operating system organizes data through the network protocol stack.
- The data is sent through the NIC to the local network.
- Routers forward the data based on the destination address.
- The data reaches the server through multiple network nodes.
- The server processes the request and returns a response.
- The browser receives the data and displays the web page.
The entire process involves addressing, routing, transmission, protocol negotiation, data encapsulation, and error handling.
1.2 What Problems Does a Computer Network Solve?
Computer networks mainly solve five types of problems.
| Problem | Solution | Typical Applications |
|---|---|---|
| Device connectivity | Wired and wireless communication links | Ethernet, Wi-Fi |
| Data addressing | IP addresses, MAC addresses, port numbers | IP, ARP, TCP |
| Data transmission | Packet switching, transport protocols | TCP, UDP |
| Path selection | Routing and forwarding | IP, OSPF, BGP |
| Data exchange | Application-layer protocols | HTTP, DNS, SMTP |
Without networks, each computer can only rely on local storage and limited external media to exchange data.
With networks, computers can share files, access remote services, use cloud platforms, conduct real-time communication, and form distributed computing systems.
1.3 Basic Goals of a Computer Network
Resource sharing
Multiple computers can share printers, storage devices, databases, and application services.
Information exchange
Users can send messages, files, images, audio, video, and other data over the network.
Distributed computing
Different computing nodes can collaborate to complete computing tasks, such as cloud computing, microservices, and distributed databases.
High availability and disaster recovery
Improve system reliability through multi-node deployment, redundant links, and failover.
Remote access
Users can access remote servers, manage devices, and use cloud resources over the network.
2. How Is a Computer Network Composed?
A complete computer network includes not only computers but also communication devices, transmission media, network protocols, and network services.
2.1 Four Basic Components of a Network
End devices are the senders or receivers of data in a network.
Common devices include:
- Clients: PCs, mobile phones, tablets.
- Servers: web servers, database servers, file servers.
- IoT devices: sensors, cameras, smart devices.
- Network management terminals: used to monitor and configure the network.
End devices usually run operating systems and connect to the network through NICs.
| Device | Core Function | Layer |
|---|---|---|
| NIC | Connects computers to the network | Physical layer, data link layer |
| Hub | Broadcasts signals to other ports | Physical layer |
| Switch | Forwards data frames based on MAC addresses | Mainly the data link layer |
| Router | Forwards packets based on IP addresses | Network layer |
| Firewall | Controls traffic based on security policies | Multiple layers |
| Wireless AP | Provides Wi-Fi access | Physical layer, data link layer |
| Modem | Performs signal conversion for specific access links | Depends on the access technology |
Note the following:
- Switches mainly handle data forwarding between devices within the same LAN.
- Routers mainly handle data forwarding between different IP networks.
- Firewalls enforce access control policies; they do not merely connect networks.
- Modern switches may also have Layer 3 routing capabilities.
Data must be transmitted through some physical or wireless medium.
| Medium | Characteristics | Typical Uses |
|---|---|---|
| Twisted pair | Low cost, easy to deploy | Ethernet |
| Optical fiber | High bandwidth, resistant to electromagnetic interference | Data centers, backbone networks |
| Coaxial cable | Good shielding characteristics | Cable TV, some access networks |
| Radio waves | No physical cabling required | Wi-Fi, cellular networks |
| Microwave links | Can be used for long-distance wireless transmission | Wireless backhaul, communication links |
| Satellite links | Wide coverage | Satellite Internet |
If two computers use different communication rules, they cannot directly and correctly understand the data sent by each other.
Network protocols specify:
- How data should be organized.
- How devices identify themselves.
- How to locate communication targets.
- How data is sent and received.
- How to handle errors when they occur.
- How both parties establish and terminate communication.
Common protocols:
| Protocol | Main Function |
|---|---|
| Ethernet | Wired LAN communication |
| Wi-Fi | Wireless LAN communication |
| IP | Network addressing and packet forwarding |
| ARP | Resolution of IPv4 addresses and link-layer addresses |
| ICMP | Network control and error reporting |
| TCP | Reliable, connection-oriented byte stream transmission |
| UDP | Connectionless datagram transmission |
| DNS | Domain name resolution |
| HTTP | Web application communication |
| TLS | Provides encryption, integrity, and identity authentication |
| DHCP | Automatically assigns network configuration |
2.2 Composition of a Typical Local Area Network
For example, a home or small office network:
flowchart TD
A["Internet"] --> B["ISP Access Network"]
B --> C["Home Router"]
C --> D["Switch"]
C --> E["Wireless Wi-Fi"]
D --> F["Desktop Computer"]
D --> G["Network Printer"]
D --> H["File Server"]
E --> I["Smartphone"]
E --> J["Laptop"]
E --> K["Smart Device"]
style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style B fill:#EAE3F7,stroke:#B7A6D9,color:#51416F
style C fill:#DCEFE5,stroke:#8EBFA5,color:#24563F
style D fill:#DCEFE5,stroke:#8EBFA5,color:#24563F
style E fill:#DCEFE5,stroke:#8EBFA5,color:#24563F
style F fill:#F1F5F9,stroke:#CBD5E1,color:#334155
style G fill:#F1F5F9,stroke:#CBD5E1,color:#334155
style H fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style I fill:#F1F5F9,stroke:#CBD5E1,color:#334155
style J fill:#F1F5F9,stroke:#CBD5E1,color:#334155
style K fill:#FFF1D6,stroke:#E6C98A,color:#76551B
In this network:
- The router connects the internal network to the carrier network.
- The switch is responsible for connecting wired devices.
- The wireless AP or the router's wireless module is responsible for connecting Wi-Fi devices.
- Each endpoint participates in network communication through an IP address.
- The router forwards packets across networks based on the routing table.
3. What Types of Computer Networks Are There?
Networks can be classified by coverage area, topology, transmission medium, and application scenario.
3.1 Classification by Coverage Area
| Network Type | English Name | Coverage | Typical Applications |
|---|---|---|---|
| Personal area network | PAN | Around an individual | Bluetooth devices |
| Local area network | LAN | Room, office, building | Enterprise internal network |
| Wireless local area network | WLAN | Wireless coverage area | Wi-Fi |
| Metropolitan area network | MAN | City-wide | City network |
| Wide area network | WAN | City, country, global | Internet, enterprise private network |
| Internet | Internet | Globally interconnected network | Global information exchange |
Local Area Network (LAN)
A LAN is usually managed by a single organization or household, and features a relatively high data transfer rate and low communication latency.
Examples:
- A company's office network.
- A school laboratory network.
- A home network.
- A network inside a data center.
Wide Area Network (WAN)
A WAN connects networks in different geographical locations through carrier networks, dedicated lines, tunnels, or other communication links.
Examples:
- The network between headquarters and branch offices.
- A network spanning cloud data centers in multiple regions.
- The Internet backbone network.
3.2 Classification by Network Topology
Network topology describes how devices are connected to one another.
Bus topology
Multiple devices share a single communication backbone.
Star topology
All terminals connect to a central switching device; this is the common LAN structure in modern Ethernet.
Ring topology
Devices form a ring-shaped connection, and data travels around the ring according to certain rules.
Mesh topology
Multiple connection paths exist between nodes, which improves redundancy and fault tolerance.
Tree topology
Multiple star networks are connected hierarchically, making it suitable for hierarchical management of large networks.
Common topologies:
flowchart TD
A["Network Topologies"]
A --> B["Bus"]
A --> C["Star"]
A --> D["Ring"]
A --> E["Mesh"]
A --> F["Tree"]
B --> B1["Shared communication backbone"]
C --> C1["Central switching device"]
D --> D1["Ring-shaped connection path"]
E --> E1["Multiple redundant paths"]
F --> F1["Hierarchical connection structure"]
style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style B fill:#DCEFE5,stroke:#8EBFA5,color:#24563F
style C fill:#DCEFE5,stroke:#8EBFA5,color:#24563F
style D fill:#EAE3F7,stroke:#B7A6D9,color:#51416F
style E fill:#EAE3F7,stroke:#B7A6D9,color:#51416F
style F fill:#FFF1D6,stroke:#E6C98A,color:#76551B
style B1 fill:#F1F5F9,stroke:#CBD5E1,color:#334155
style C1 fill:#F1F5F9,stroke:#CBD5E1,color:#334155
style D1 fill:#F1F5F9,stroke:#CBD5E1,color:#334155
style E1 fill:#F1F5F9,stroke:#CBD5E1,color:#334155
style F1 fill:#F1F5F9,stroke:#CBD5E1,color:#334155
Modern networks usually combine hierarchical star, tree, and mesh structures.
3.3 Classification by Transmission Method
Wired networks
Data is transmitted over media such as twisted pair cable and optical fiber.
Advantages:
- Stable communication.
- Generally lower latency.
- Higher bandwidth.
- Less susceptible to wireless interference.
Wireless networks
Data is transmitted via radio waves.
Common technologies:
- Wi-Fi.
- Bluetooth.
- 4G.
- 5G.
- Satellite communication.
The advantages are flexible deployment and strong mobility, but performance may be affected by distance, obstacles, interference, and signal strength.
IV. Network Layering Models: How Is Complex Communication Broken Down?
Computer networks involve a large number of different devices and protocols. If a single complex program had to handle every function, the system would be difficult to design, develop, and maintain.
For this reason, networks adopt a layered design approach.
Each layer handles a relatively independent set of functions and cooperates with adjacent layers through interfaces.
4.1 The OSI Seven-Layer Model
OSI is the Open Systems Interconnection reference model, which divides network communication into seven layers.
| Layer | Name | Primary Responsibilities | Typical Protocols or Technologies |
|---|---|---|---|
| Layer 7 | Application Layer | Provides network services for applications | HTTP, DNS, SMTP |
| Layer 6 | Presentation Layer | Data representation, encoding, encryption, and compression | ASN.1, data encoding, encryption mechanisms |
| Layer 5 | Session Layer | Manages session establishment and maintenance | Session control mechanisms |
| Layer 4 | Transport Layer | End-to-end transmission, multiplexing, and reliability | TCP, UDP |
| Layer 3 | Network Layer | Logical addressing and routing | IPv4, IPv6, ICMP |
| Layer 2 | Data Link Layer | Frame transmission, MAC addressing, and link control | Ethernet, Wi-Fi |
| Layer 1 | Physical Layer | Transmits bitstreams | Optical fiber, cable, wireless signals |
OSI is a reference model for understanding networks. The functional boundaries of real network protocols do not necessarily correspond exactly one-to-one with the seven-layer model.
4.2 TCP/IP Four-Layer Model
The Internet actually mainly adopts the TCP/IP protocol suite.
| TCP/IP Layer | Corresponding OSI Layer | Main Protocols |
|---|---|---|
| Application Layer | Application Layer, Presentation Layer, Session Layer | HTTP, DNS, SSH |
| Transport Layer | Transport Layer | TCP, UDP |
| Internet Layer | Network Layer | IP, ICMP |
| Network Interface Layer | Data Link Layer, Physical Layer | Ethernet, Wi-Fi |
Some materials sometimes use a five-layer teaching model, splitting the network interface layer into the data link layer and the physical layer.
4.3 TCP/IP Protocol Architecture
flowchart TB
subgraph APP["Application Layer"]
A1["HTTP / HTTPS"]
A2["DNS"]
A3["SSH / SMTP"]
end
subgraph TRANS["Transport Layer"]
B1["TCP"]
B2["UDP"]
end
subgraph NET["Internet Layer"]
C1["IPv4 / IPv6"]
C2["ICMP"]
end
subgraph LINK["Network Interface Layer"]
D1["Ethernet"]
D2["Wi-Fi"]
end
A1 --> B1
A2 --> B2
A3 --> B1
A3 --> B2
B1 --> C1
B2 --> C1
C1 --> D1
C1 --> D2
C2 --> D1
style A1 fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style A2 fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style A3 fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style B1 fill:#DCEFE5,stroke:#8EBFA5,color:#24563F
style B2 fill:#DCEFE5,stroke:#8EBFA5,color:#24563F
style C1 fill:#EAE3F7,stroke:#B7A6D9,color:#51416F
style C2 fill:#EAE3F7,stroke:#B7A6D9,color:#51416F
style D1 fill:#FFF1D6,stroke:#E6C98A,color:#76551B
style D2 fill:#FFF1D6,stroke:#E6C98A,color:#76551B
style APP fill:#F6FAF7,stroke:#DCE9E0,color:#334155
style TRANS fill:#F6FAF7,stroke:#DCE9E0,color:#334155
style NET fill:#F6FAF7,stroke:#DCE9E0,color:#334155
style LINK fill:#F6FAF7,stroke:#DCE9E0,color:#334155
4.4 Why Does a Network Need Layering?
Layered architecture mainly solves the following problems.
Reduce complexity
Each layer is responsible only for specific responsibilities and does not need to understand all implementation details of other layers.
Improve replaceability
The application layer can use different transport protocols, and the network interface layer can also replace different communication media.
Promote standardization
Devices and software from different vendors implement according to unified protocols and can communicate with each other.
Facilitate fault localization
When a problem occurs, it can be troubleshooted layer by layer:
- Application layer: Is the HTTP request correct?
- Transport layer: Is the TCP connection established?
- Network layer: Is the IP reachable?
- Data link layer: Is MAC address resolution working properly?
- Physical layer: Are the network cable, fiber optic, and wireless signals working properly?
4.5 Peer Communication and Encapsulation
Layered networking has an important idea: communication entities at the same layer logically follow the same protocol rules.
For example:
- Client TCP and server TCP cooperate through the TCP protocol.
- Client IP and server IP follow the IP protocol.
- The Ethernet or Wi-Fi interfaces at both ends transmit data according to the corresponding link-layer protocol.
But actual data does not jump directly from one computer's application layer to another computer's application layer; it passes through the local protocol stack, the network link, and the peer's protocol stack.
5. Core Principles of Network Communication: How Does Data Travel from One Computer to Another?
Network communication can be understood as the process in which data is progressively encapsulated, transmitted, and decapsulated across different protocol layers.
5.1 Data Encapsulation
Suppose a client sends an HTTP request to a server.
The application layer first generates HTTP data.
The transport layer adds a TCP or UDP header to the data.
The network layer adds an IP header.
The data link layer adds a frame header and frame trailer.
The physical layer encodes the data into signals that can be transmitted over the medium.
This process is called encapsulation.
flowchart TD
A["Application data HTTP"] --> B["Add transport layer header"]
B --> C["TCP Segment"]
C --> D["Add IP header"]
D --> E["IP Packet"]
E --> F["Add link layer header and trailer"]
F --> G["Ethernet Frame"]
G --> H["Encode into bitstream"]
H --> I["Transmit over physical medium"]
style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style B fill:#DCEFE5,stroke:#8EBFA5,color:#24563F
style C fill:#DCEFE5,stroke:#8EBFA5,color:#24563F
style D fill:#EAE3F7,stroke:#B7A6D9,color:#51416F
style E fill:#EAE3F7,stroke:#B7A6D9,color:#51416F
style F fill:#FFF1D6,stroke:#E6C98A,color:#76551B
style G fill:#FFF1D6,stroke:#E6C98A,color:#76551B
style H fill:#F1F5F9,stroke:#CBD5E1,color:#334155
style I fill:#F1F5F9,stroke:#CBD5E1,color:#334155
Names of data units at different layers:
| Layer | Data Unit | Description |
|---|---|---|
| Application layer | Message | Application message |
| Transport layer | Segment / Datagram | TCP segment or UDP datagram |
| Network layer | Packet | IP packet |
| Data link layer | Frame | Data frame |
| Physical layer | Bits | Bitstream |
These names are common terms; specific protocols have their own data unit definitions.
5.2 Data Decapsulation
The receiving end performs the reverse process:
- The physical layer receives signals and recovers the bitstream.
- The data link layer parses the data frame.
- The network layer checks the IP header and destination address.
- The transport layer delivers data based on the port number.
- The application layer parses the application protocol content.
flowchart TD
A["Receive physical signal"] --> B["Recover bitstream"]
B --> C["Data link layer parses frame"]
C --> D["Network layer parses IP packet"]
D --> E["Transport layer parses TCP or UDP"]
E --> F["Application layer parses protocol message"]
F --> G["Deliver to application"]
G --> H["Application processes result"]
style A fill:#F1F5F9,stroke:#CBD5E1,color:#334155
style B fill:#F1F5F9,stroke:#CBD5E1,color:#334155
style C fill:#FFF1D6,stroke:#E6C98A,color:#76551B
style D fill:#EAE3F7,stroke:#B7A6D9,color:#51416F
style E fill:#DCEFE5,stroke:#8EBFA5,color:#24563F
style F fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style G fill:#DCEFE5,stroke:#8EBFA5,color:#24563F
style H fill:#F1F5F9,stroke:#CBD5E1,color:#334155
5.3 Why Is Encapsulation Needed?
The value of encapsulation lies in the separation of responsibilities.
For example:
- HTTP does not need to care about how the network card sends bits.
- TCP does not need to understand the specific meaning of HTML on a web page.
- IP does not need to know whether the application is using HTTP or SSH.
- Ethernet does not need to know the business content inside an IP packet.
Each layer only handles the information it needs to understand.
This is also an important reason why network protocols can continue to be extended.
Six: Network Addressing: How Do Devices Find Each Other?
For data to reach the target device correctly, the network must solve the addressing problem.
The three most important types of identifiers in network communication are:
- MAC address: identifies a link-layer interface.
- IP address: identifies a logical interface in the network.
- Port number: identifies a communication endpoint on a host.
6.1 MAC Address
MAC addresses are typically used for data link layer communication.
A typical Ethernet MAC address is 48 bits long and is usually expressed as six groups of hexadecimal digits:
00:1A:2B:3C:4D:5E
MAC addresses are mainly used for frame delivery on the same link or within the same local area network.
A switch can learn the correspondence between MAC addresses and switch ports, and forward data frames according to the MAC address table.
View the MAC address of a Linux network card:
ip link
Or:
ip addr
Note that MAC addresses can be modified or spoofed by software; they are not absolutely immutable proof of device identity.
6.2 IP Address
IP addresses are used for network layer addressing, enabling data packets to be transmitted between different networks.
Currently, IPv4 and IPv6 are the main versions in use.
IPv4
An IPv4 address is 32 bits long.
For example:
192.168.1.100
An IPv4 address consists of four decimal numbers, each corresponding to 8 bits.
IPv6
An IPv6 address is 128 bits long.
For example:
2001:db8:1234::100
IPv6 is mainly used to solve problems such as the insufficient address space of IPv4, and it supports a larger address space and new network capabilities.
6.3 Public IP and Private IP
IPv4 addresses can be divided according to their scope of use.
Common private address ranges:
| Address range | CIDR | Typical use |
|---|---|---|
| 10.0.0.0 - 10.255.255.255 | 10.0.0.0/8 | Enterprise intranet |
| 172.16.0.0 - 172.31.255.255 | 172.16.0.0/12 | Enterprise and data center |
| 192.168.0.0 - 192.168.255.255 | 192.168.0.0/16 | Home and small networks |
Public addresses can be used in the global Internet routing system, but specific addresses are also subject to registration and routing policy constraints.
Private addresses cannot be used directly as ordinary public destination addresses on the global Internet.
6.4 Subnet Mask and CIDR
The subnet mask is used to distinguish the network portion and the host portion of an IP address.
For example:
IP address: 192.168.1.100
Subnet mask: 255.255.255.0
It can also be written as:
192.168.1.100/24
Here, /24 indicates that the first 24 bits belong to the network prefix.
This network corresponds to:
Network address: 192.168.1.0
Broadcast address: 192.168.1.255
In a traditional IPv4 subnet, the addresses that can usually be assigned to hosts are:
192.168.1.1 - 192.168.1.254
This is the common case for an ordinary /24 subnet. Point-to-point links, special-purpose addresses, and other prefix lengths need to be analyzed separately.
CIDR can flexibly represent network address ranges:
| CIDR | Total addresses |
|---|---|
/8 |
16,777,216 |
/16 |
65,536 |
/24 |
256 |
/25 |
128 |
/26 |
64 |
/30 |
4 |
/32 |
1 |
6.5 How Do IP Addresses and MAC Addresses Work Together?
Assume:
- Client IP: 192.168.1.10
- Client MAC: AA:AA:AA:AA:AA:AA
- Gateway IP: 192.168.1.1
- Gateway MAC: BB:BB:BB:BB:BB:BB
- Server IP: 203.0.113.10
When the client wants to access the server, it first determines whether the destination IP is on the same IP subnet as itself.
If the destination is not on the local subnet, the client hands the data to the default gateway.
In this case:
- The IP destination address is still the server IP.
- The Ethernet destination MAC address is the default gateway's MAC address.
After the router receives the frame, it strips the original link-layer encapsulation, looks up the route based on the destination IP, and re-encapsulates the frame for the next link.
So the IP address is responsible for locating the destination across networks, while the MAC address is responsible for delivering the frame on the current link.
6.6 ARP Address Resolution
In an IPv4 local area network, ARP is used to look up the corresponding link-layer address for a destination IPv4 address.
For example:
Destination IP: 192.168.1.1
Destination MAC: BB:BB:BB:BB:BB:BB
How ARP works:
flowchart TD
A["Host needs to send an IPv4 packet"] --> B["Check the ARP cache"]
B --> C{"Is there a MAC address for the destination IP?"}
C -->|Yes| D["Read the MAC address from the cache"]
C -->|No| E["Send an ARP request broadcast"]
E --> F["Target host recognizes its own IP"]
F --> G["Send an ARP reply"]
G --> H["Sender updates the ARP cache"]
D --> I["Build the Ethernet frame"]
H --> I
I --> J["Send the data frame"]
style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style B fill:#DCEFE5,stroke:#8EBFA5,color:#24563F
style C fill:#FFF1D6,stroke:#E6C98A,color:#76551B
style D fill:#DCEFE5,stroke:#8EBFA5,color:#24563F
style E fill:#EAE3F7,stroke:#B7A6D9,color:#51416F
style F fill:#F1F5F9,stroke:#CBD5E1,color:#334155
style G fill:#F1F5F9,stroke:#CBD5E1,color:#334155
style H fill:#DCEFE5,stroke:#8EBFA5,color:#24563F
style I fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style J fill:#DCEFE5,stroke:#8EBFA5,color:#24563F
View the ARP cache:
ip neigh
Note the following:
- ARP is used for IPv4.
- IPv6 uses the Neighbor Discovery Protocol (NDP) rather than ARP.
- If the destination is not on the local subnet, what usually needs to be resolved is the MAC address of the default gateway, not the MAC address of the remote server.
7. Routing and Data Forwarding: How Does Data Cross Multiple Networks?
IP addresses solve the problem of locating the destination, but data still needs to find a path that can reach the destination network.
This is the routing mechanism.
7.1 What Is Routing?
Routing is the process of choosing a forwarding path for a packet based on the destination network address.
Routers typically maintain a routing table.
The routing table records:
- The destination network prefix.
- The next-hop address.
- The outgoing interface.
- The route source.
- The route priority or metric.
View the Linux routing table:
ip route
Example:
default via 192.168.1.1 dev eth0
192.168.1.0/24 dev eth0 proto kernel scope link
Meaning:
- Default route: traffic that matches no more specific route is sent to
192.168.1.1. - Local subnet:
192.168.1.0/24is directly connected viaeth0.
7.2 Longest Prefix Match
When selecting a route, routers generally apply the longest prefix match principle.
For example:
| Destination prefix | Next hop |
|---|---|
| 0.0.0.0/0 | Router A |
| 10.0.0.0/8 | Router B |
| 10.1.0.0/16 | Router C |
| 10.1.2.0/24 | Router D |
If the destination address is:
10.1.2.100
then the selected route is:
10.1.2.0/24
because it is the longest network prefix matching that destination address.
7.3 Static Routing and Dynamic Routing
Static Routing
Manually configured by an administrator.
Advantages:
- Simple configuration.
- Predictable behavior.
- Suitable for smaller networks with stable topology.
Disadvantages:
- Requires manual maintenance when the network changes.
- Higher management cost for large-scale networks.
- Lacks automatic path adjustment capability.
Dynamic Routing
Routers exchange routing information through routing protocols.
Common protocols:
| Protocol | Function |
|---|---|
| RIP | Distance-vector-based routing protocol |
| OSPF | Link-state interior gateway protocol |
| IS-IS | Link-state routing protocol |
| BGP | Exchanges routing information between autonomous systems |
7.4 Routing Forwarding Process
Assume data travels from a client through multiple routers to a remote server.
flowchart TD
A["Client generates IP packet"] --> B["Send to default gateway"]
B --> C["Router receives packet"]
C --> D["Check destination IP address"]
D --> E["Look up routing table"]
E --> F{"Is there a matching route?"}
F -->|Yes| G["Select longest prefix match route"]
F -->|No| H["Check default route"]
H --> I{"Does a default route exist?"}
I -->|Yes| G
I -->|No| J["Drop packet and possibly return an error"]
G --> K["Determine next hop and egress interface"]
K --> L["Re-encapsulate link-layer frame"]
L --> M["Forward to next hop"]
M --> N{"Has the destination network been reached?"}
N -->|No| C
N -->|Yes| O["Deliver to destination host"]
style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style B fill:#DCEFE5,stroke:#8EBFA5,color:#24563F
style C fill:#DCEFE5,stroke:#8EBFA5,color:#24563F
style D fill:#EAE3F7,stroke:#B7A6D9,color:#51416F
style E fill:#EAE3F7,stroke:#B7A6D9,color:#51416F
style F fill:#FFF1D6,stroke:#E6C98A,color:#76551B
style G fill:#DCEFE5,stroke:#8EBFA5,color:#24563F
style H fill:#F1F5F9,stroke:#CBD5E1,color:#334155
style I fill:#FFF1D6,stroke:#E6C98A,color:#76551B
style J fill:#FCE8E8,stroke:#E5AAAA,color:#8A3D3D
style K fill:#F1F5F9,stroke:#CBD5E1,color:#334155
style L fill:#F1F5F9,stroke:#CBD5E1,color:#334155
style M fill:#DCEFE5,stroke:#8EBFA5,color:#24563F
style N fill:#FFF1D6,stroke:#E6C98A,color:#76551B
style O fill:#DCEFE5,stroke:#8EBFA5,color:#24563F
7.5 What Information Changes During Route Forwarding?
In ordinary route forwarding:
- The source IP address usually remains unchanged.
- The destination IP address usually remains unchanged.
- The original Ethernet frame is removed.
- The next link uses a new data link layer frame.
- The IPv4 TTL is decremented.
- The IPv4 header checksum is updated accordingly.
If NAT, tunnel encapsulation, or other network functions are present, IP addresses and packet structure may also change.
7.6 NAT Network Address Translation
Home and enterprise networks often use NAT.
For example:
Internal device: 192.168.1.10
Router public address: 198.51.100.20
Multiple internal devices can share a public IPv4 address through a router.
A common form is NAPT, also called PAT, which distinguishes different internal connections through port mapping.
For example:
192.168.1.10:50001
|
v
198.51.100.20:30001
NAT primarily addresses the shortage of IPv4 address resources and network boundary translation issues, but it is not a complete network security mechanism.
8. Transport Layer: How Do TCP and UDP Transmit Data?
The network layer is responsible for delivering packets to the destination host, but multiple applications usually run on a host.
The transport layer needs to further solve the problem of data delivery between processes.
8.1 Port Numbers
Port numbers are logical numbers used by the transport layer to identify communication endpoints.
Both TCP and UDP use 16-bit port numbers.
Range:
| Range | Classification | Description |
|---|---|---|
| 0 - 1023 | Well-known ports | Common standard services |
| 1024 - 49151 | Registered ports | Registered services or applications |
| 49152 - 65535 | Dynamic or private ports | Common ephemeral port range for clients |
Common ports:
| Port | Protocol | Purpose |
|---|---|---|
| 20, 21 | TCP | FTP |
| 22 | TCP | SSH |
| 25 | TCP | SMTP |
| 53 | UDP/TCP | DNS |
| 80 | TCP | HTTP |
| 443 | TCP, and UDP in some scenarios | HTTPS, QUIC |
| 3306 | TCP | MySQL |
| 5432 | TCP | PostgreSQL |
| 6379 | TCP | Redis |
The actual use of port numbers is also affected by application configuration and service deployment.
8.2 What is TCP?
TCP is a connection-oriented, reliable byte-stream transport protocol.
Main characteristics:
- Communicates after establishing a connection.
- Provides ordered data delivery.
- Uses sequence numbers to track data.
- Uses an acknowledgment mechanism.
- Uses a retransmission mechanism.
- Provides flow control.
- Provides congestion control.
- Provides a continuous byte stream to applications rather than preserving application message boundaries.
TCP is suitable for scenarios that require reliable, ordered data transmission, such as:
- Web access.
- File transfer.
- Database connections.
- SSH remote management.
8.3 TCP three-way handshake
Establishing a TCP connection usually requires a three-way handshake.
sequenceDiagram
participant C as Client
participant S as Server
C->>S: SYN
Note over C,S: Client requests connection establishment
S->>C: SYN + ACK
Note over C,S: Server acknowledges and synchronizes its own sequence number
C->>S: ACK
Note over C,S: Client acknowledges server response
Note over C,S: TCP connection established
Purpose of the three-way handshake:
- The client sends SYN to indicate that it wants to establish a connection.
- The server returns SYN and ACK, acknowledging the client's request while sending its own synchronization information.
- The client sends ACK to acknowledge the server's response.
After the handshake is complete, both parties can begin transmitting application data.
8.4 TCP four-way teardown
Normally closing a TCP connection usually involves both parties independently closing their sending directions.
sequenceDiagram
participant C as Client
participant S as Server
C->>S: FIN
Note over C,S: Client requests to close its sending direction
S->>C: ACK
Note over C,S: Server acknowledges
Note over C,S: The server may continue sending remaining data
S->>C: FIN
Note over C,S: Server requests to close its sending direction
C->>S: ACK
Note over C,S: Client acknowledges
TCP is a full-duplex protocol, and the two directions can be closed independently, so the close requests for the two directions usually need to be acknowledged separately.
When the client actively closes the connection, it usually enters the TIME_WAIT state for a period of time to handle old packets that may arrive late and to ensure that the final acknowledgment can be processed reliably.
8.5 TCP reliable transmission mechanisms
TCP mainly provides reliable transmission through the following mechanisms:
Sequence numbers
Used to mark byte positions in the sent data stream.
Acknowledgment mechanism
The receiver uses ACK to tell the sender which range of data has been successfully received.
Timeout retransmission
The sender waits for an acknowledgment; if a valid acknowledgment is not received before the timeout, it retransmits the data according to the retransmission mechanism.
Fast retransmission
When certain conditions are met, the sender can retransmit lost data early based on signals such as duplicate ACKs.
Sliding window
Allows the sender to continuously send a certain amount of data without waiting for an acknowledgment for each packet.
Flow control
Adjusts the sending rate according to the receiver's receiving capacity to prevent the receiver's buffer from being filled with excessive data.
Congestion control
Adjust the sending rate according to network congestion conditions, so the network does not keep deteriorating due to excessive traffic.
8.6 What is UDP?
UDP is a connectionless datagram transport protocol.
Main characteristics:
- No connection needs to be established.
- Does not guarantee reliable delivery.
- Does not guarantee that datagrams arrive in order.
- Does not provide TCP-style retransmission and flow control.
- The protocol header is relatively small.
- Preserves datagram boundaries.
Common applications:
- DNS queries.
- Real-time audio and video.
- Online games.
- Some monitoring and telemetry systems.
- The underlying transport for QUIC.
UDP itself does not provide reliability, but applications can implement acknowledgment, retransmission, congestion control, and data ordering on top of UDP themselves.
8.7 TCP vs. UDP
| Aspect | TCP | UDP |
|---|---|---|
| Connection mode | Connection-oriented | Connectionless |
| Reliability | Provides a reliable, ordered byte stream | Does not guarantee reliable delivery |
| Data boundaries | Does not preserve application message boundaries | Preserves datagram boundaries |
| Ordering | Provides an ordered byte stream to the application | Does not guarantee order |
| Flow control | Yes | No built-in TCP-style flow control |
| Congestion control | Yes | No built-in TCP-style congestion control |
| Overhead | Relatively high | Relatively low |
| Typical applications | Web, file transfer, SSH | DNS, real-time audio and video, QUIC |
8.8 Why are port numbers needed?
Suppose a server runs three services:
Web service: 80
SSH service: 22
Database: 3306
They can share the same server IP address.
The client locates the server by the destination IP address, and then locates the specific service by the destination port number.
Communication is usually described jointly by the following information:
Protocol
Source IP
Source port
Destination IP
Destination port
This kind of information is commonly called the five-tuple of a network connection.
9. Application Layer: DNS, HTTP, and HTTPS
Application layer protocols directly serve applications and specify how business data is exchanged.
9.1 DNS: How are domain names converted to IP addresses?
People are used to accessing websites by domain name:
www.example.com
But IP networks need IP addresses to locate destinations.
DNS is responsible for resolving domain names into the corresponding resource records, such as A records and AAAA records.
Common DNS records:
| Record type | Purpose |
|---|---|
| A | Domain name maps to an IPv4 address |
| AAAA | Domain name maps to an IPv6 address |
| CNAME | Alias record |
| MX | Mail exchange server |
| NS | Authoritative name server |
| TXT | Text information record |
| PTR | Reverse lookup record |
| SRV | Service location record |
9.2 The DNS resolution process
When a browser visits a website, it usually needs to obtain the destination address first.
flowchart TD
A["Application requests domain access"] --> B["Check browser DNS cache"]
B --> C{"Is there a valid record?"}
C -->|Yes| D["Return cached IP"]
C -->|No| E["Check OS cache"]
E --> F{"Is there a valid record?"}
F -->|Yes| D
F -->|No| G["Query recursive DNS server"]
G --> H{"Does recursive server have cache?"}
H -->|Yes| I["Return resolution result"]
H -->|No| J["Query root DNS server"]
J --> K["Query TLD DNS server"]
K --> L["Query authoritative DNS server"]
L --> M["Retrieve DNS record"]
M --> N["Recursive server caches result"]
N --> I
I --> O["Client obtains target IP"]
D --> O
style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style B fill:#DCEFE5,stroke:#8EBFA5,color:#24563F
style C fill:#FFF1D6,stroke:#E6C98A,color:#76551B
style D fill:#DCEFE5,stroke:#8EBFA5,color:#24563F
style E fill:#DCEFE5,stroke:#8EBFA5,color:#24563F
style F fill:#FFF1D6,stroke:#E6C98A,color:#76551B
style G fill:#EAE3F7,stroke:#B7A6D9,color:#51416F
style H fill:#FFF1D6,stroke:#E6C98A,color:#76551B
style I fill:#DCEFE5,stroke:#8EBFA5,color:#24563F
style J fill:#F1F5F9,stroke:#CBD5E1,color:#334155
style K fill:#F1F5F9,stroke:#CBD5E1,color:#334155
style L fill:#F1F5F9,stroke:#CBD5E1,color:#334155
style M fill:#EAE3F7,stroke:#B7A6D9,color:#51416F
style N fill:#DCEFE5,stroke:#8EBFA5,color:#24563F
style O fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
This shows a typical recursive resolution process. Actual resolution may be affected by browser cache, OS cache, DNS forwarders, CDNs, encrypted DNS, and other resolution mechanisms.
Query DNS:
nslookup example.com
Or:
dig example.com
View detailed records:
dig example.com A
dig example.com AAAA
dig example.com MX
9.3 What Is HTTP?
HTTP is an application-layer protocol used to exchange resources between clients and servers.
Common applications:
- Browsers accessing web pages.
- Mobile apps calling backend APIs.
- HTTP communication between microservices.
- Cloud platforms providing web services.
An HTTP request usually includes:
- Request method.
- Request path.
- Request headers.
- Optional request body.
Example: