[Linux] Network Foundation (1)–Preparing for socket programming

Hits: 0

Network Foundation (1)

▶ Understand the background of network development

🎄 Network Development

  • Independent mode: In independent mode, there is no network, and the data of each computer is independent of each other. At this time, if you want to implement business, you need to implement each business logic in turn.
  • Network interconnection: Under the network interconnection, the data of each independent computer is centrally managed through the server, and the business logic can be switched at any time.
  • Local Area Network (LAN): With the increase in the number of computers, computers in an area are connected together through switches and routers.
    In fact, the so-called local area network and wide area network are only relative concepts, and our country’s wide area network can also be regarded as a large local area network.

▶ [Network Protocol]

🎄Understand the protocol

What is an agreement? An agreement is an agreement reached by both parties, and when we communicate with each other, every byte we pass has a different meaning. The essence of the protocol is a kind of agreement, and the so-called network protocol is to complete the performance of each host protocol participating in the communication through the expression of data.
For example, when making a phone call, from the user’s point of view, it is considered to be communicating directly with the other party; but from the programmer’s point of view, the two parties do not communicate directly. We simplify the level here and divide the communication process into the language layer and communication equipment. Layer, then on the language layer, the Chinese protocol is required for both users to communicate in Chinese, and the data stream reading between the phones requires the construction of a telephone protocol.

With this layered approach, if the communication device is changed, for example, a radio conversation is used, then only the protocol at the communication device layer needs to be changed, and if the user wants to talk in English, only the protocol at the language layer needs to be changed. Under the software system with a layered structure, any layer of protocols can be easily replaced at any time .
Of course, in this example, we have simplified the layers, and the actual network communication will be more complex and need to be divided into more layers.
The biggest benefit of this layering is “encapsulation”, and the network is divided into the [OSI seven-layer model] and the TCP/IP five-layer model in more detail.

🎄OSI seven-layer model

  • The OSI (Open System Interconnection, Open System Interconnection) seven-layer network model is called the Open System Interconnection Reference Model, which is a logical definition and specification.
  • OSI logically divides the network 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 the three concepts of service, interface and protocol are clearly distinguished, the concept is clear, and the theory is relatively complete. Through seven hierarchical structural models, different systems and different networks can achieve reliable communication;
  • However, it is complex and impractical; so we will focus on the TCP/IP four-layer structure model.

🎄TCP/IP five-layer (four-layer) structure model

TCP/IP is synonymous with a set of protocols. It also includes many protocols, which form the TCP/IP protocol suite.
The TCP/IP communication protocol adopts a 5-layer hierarchical structure, and each layer calls the protocol provided by the next layer. Network to complete your own needs:

  • Physical layer : responsible for the transmission of optical/electrical signals. For example, the common network cable (twisted pair) of Ethernet, the coaxial cable used in early Ethernet (now mainly used for cable TV), optical fiber, and the current wifi wireless network The use of electromagnetic waves, etc. belong to the concept of the physical layer. The ability of the physical layer determines 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 cable 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. Switches work at the data link layer.
  • Network layer : Responsible for address management and routing. For example, in the IP protocol, a host is identified by an IP address, and a route (routing) for data transmission between two hosts is planned through a routing table. Router (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 reliably sent 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 the physical layer less. Therefore, it can often be called the TCP/IP four-layer model.
    Generally speaking:
  • For a host, its operating system kernel implements the 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.
    These are not absolute. For example, many switches also implement network layer forwarding; many routers also implement some transport layer content (such as port forwarding);

▶Address management in the network

In network communication, in order to find the communication object, it is necessary to manage the address so that the communication can be sent to the target accurately.

🎄 IP address

There are two versions of IP protocol, IPv4and IPv6. Subsequent references to IP protocol, unless otherwise specified, refer to by default IPv4.

  • IP address is the address used to identify different hosts in the network in the IP protocol;
  • For IPv4example, the IP address is a 4-byte, 32-bit integer;
  • We also usually use “dotted decimal” strings to represent IP addresses, such as; each number separated by a dot represents a byte, and the range is 0 – 255;

🎄MAC address

  • MAC addresses are used to identify connected nodes in the data link layer;
  • The length is 48 bits and 6 bytes. It is usually expressed in the form of a hexadecimal number plus a colon (for example: 08:00:27:03:fb:19)
  • The MAC address is determined when the network card leaves the factory and cannot be modified. The mac address is usually unique (the mac address in the virtual machine is not the real mac address and may conflict; some network cards also support the user to configure the mac address).

▶Basic process of network transmission

🎄 File transfer between two hosts in the same [network segment]

The process of communication between two computers through TCP/IP protocol is as follows:

🎄Packet encapsulation and demultiplexing

  • Different protocol layers have different names for data packets, which 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 protocol must add a data header, which is called encapsulation.
  • The header information contains some 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 the protocol strips the corresponding header, and according to the “upper-layer protocol field” in the header, the data is handed over to the corresponding upper-layer protocol for processing.
  • Simply put, the process of adding headers to the data sent by the client from the application layer to the data link layer layer by layer is encapsulation; while the server side removes the header from the data received from the data link layer to the application layer. Just share.
    The process of data encapsulation: the process of
    data allocation:

You may also like...

Leave a Reply

Your email address will not be published.