[Linux] Network Programming Network Basics (C++)

Table of contents

1. Computer network background

2. Understand "agreement"

3. First understanding of network protocols

【3.1】Protocol layering

【3.2】OSI seven-layer model

【3.3】TCP/IP five-layer (or four-layer) model

4. Basic process of network transmission

【4.1】Network transmission flow chart

【4.2】Data packet encapsulation and demultiplexing

5. Address management in the network


1. Computer network background

[Independent mode] Computers are independent of each other.

[Network interconnection] Multiple computers are connected together to complete data sharing.

[Local Area Network] There are more computers, which are connected together through switches and routers.

[Wide Area Network (WAN)] connects computers thousands of miles apart.

        The so-called "local area network" and "wide area network" are just relative concepts. For example, our wide area network with "Chinese characteristics" can also be regarded as a relatively large local area network.

2. Understand "agreement"

        "Agreement" is an agreement .

        The transmission media between computers are optical signals and electrical signals. Information such as 0 and 1 is represented by "frequency" and "strength". In order to transmit various different information, the data format of both parties needs to be agreed upon.

[Thinking] Is it enough to just agree on a protocol between two hosts communicating?

  • There are many computer manufacturers.

  • There are also many computer operating systems.

  • There are still many computer network hardware devices.

  • How to enable computers produced by different manufacturers to communicate smoothly with each other? Someone needs to stand up and agree on a common standard for everyone to abide by. This is the network protocol .

3. First understanding of network protocols

【3.1】Protocol layering

        Our protocol has only two layers; but actual network communication will be more complex and requires more layers. The biggest advantage of layering is "encapsulation". Object-oriented example .

【3.2】OSI seven-layer model

        The OSI (Open System Interconnection) seven-layer network model is called the Open System Interconnection Reference Model, which is a logical definition and specification.

        The network is logically divided into 7 layers. Each layer has related and corresponding physical devices, such as routers and switches;

        The OSI seven-layer model is a framework design method, and its main function is to help different types of hosts realize data transmission.

        Its biggest advantage is that it clearly distinguishes the three concepts of service, interface and protocol. The concept is clear and the theory is relatively complete. Through seven hierarchical structural models, reliable communication can be achieved between different systems and different networks.

        However, it is both complicated and impractical; so we will explain it according to the TCP/IP four-layer model.

【3.3】TCP/IP five-layer (or four-layer) model

        TCP/IP is synonymous with a group of protocols, which also includes many protocols that form the TCP/IP protocol cluster.

        The TCP/IP communication protocol adopts a 5-layer hierarchical structure. Each layer calls the network provided by the next layer to complete its own needs.

  • Physical layer: Responsible for the transmission method of optical/electrical signals. For example, the current common network cables (twisted pairs) of Ethernet, the coaxial cables used by early Ethernet (now mainly used for cable TV), optical fibers, and current wifi wireless networks The use of electromagnetic waves and so on all belong to the concept of the physical layer. The capabilities of the physical layer determine the maximum transmission rate, transmission distance, anti-interference, etc. The hub (Hub) works at the physical layer.

  • Data link layer: Responsible for the transmission and identification of data frames between devices. For example, the driver of the network card device, frame synchronization (that is, what signal is detected from the network line is counted as the start of a new frame), conflict detection (if a conflict is detected Automatic retransmission), data error checking, etc. There are standards such as Ethernet, Token Ring, and Wireless LAN. The switch (Switch) works at the data link layer.

  • Network layer: Responsible for address management and routing selection. For example, in the IP protocol, a host is identified by its IP address, and the data transmission line (routing) between two hosts is planned through the routing table. Router ) works at the network layer.

  • Transport layer: Responsible for data transmission between two hosts. Such as Transmission Control Protocol (TCP), which can ensure that data is sent reliably from the source host to the target host.

  • Application layer: Responsible for communication between applications, such as Simple Email Transfer (SMTP), File Transfer Protocol (FTP), Network Remote Access Protocol (Telnet), etc. Our network programming is mainly for the application layer.

        We consider less about the physical layer. Therefore, it can often be called the TCP/IP four-layer model.

Generally speaking :

  • For a host, its operating system kernel implements content from the transport layer to the physical layer.

  • For a router, it implements from the network layer to the physical layer.

  • For a switch, it implements from the data link layer to the physical layer.

  • For hubs, it only implements the physical layer.

4. Basic process of network transmission

【4.1】Network transmission flow chart

File transfer between two hosts in the same network segment:

Schematic diagram of two computers communicating through TCP/IP protocol:

        File transfer between hosts across network segments. Data must pass through one or more routers when transferring from one computer to another.

        Protocol header: Each layer of the protocol has it. The final expression of each protocol is that the protocol must have a header. The protocol is usually expressed through the header. Each piece of data is ultimately sent or in different protocols. Have your own masthead!

        LAN: Two LAN hosts can communicate directly. Each computer has its own name, each host has its own network card, and each network card has its own address (MAC address).

        Message: header + payload.

        In network protocols, we can think of the same layer protocol as communicating directly, or we can understand it as downward delivery. These are two different perceptions. File transfer between hosts across network segments. Data must pass through one or more routers when transferring from one computer to another.

【4.2】Data packet encapsulation and demultiplexing

  • Different protocol layers have different names for data packets. They are called segments at the transport layer, datagrams at the network layer , and frames at the link layer .

  • When application layer data is sent to the network through the protocol stack, each layer of protocol must add a data header , which is called encapsulation .

  • The header information contains information such as how long the header is, how long the payload is, and what the upper layer protocol is.

  • The data is encapsulated into frames and sent to the transmission medium. After reaching the destination host, each layer of protocol strips off the corresponding header, and hands the data to the corresponding upper layer protocol for processing according to the "upper layer protocol field" in the header.

5. Address management in the network

Guess you like

Origin blog.csdn.net/lx473774000/article/details/132769939