To read more articles in this series please visit myMaking a blog, sample code please visithere.

Net module

The NET module is used to create stream-based TCP or IPC servers (net.createserver ()) and clients (net.createconnection ()).

Net module is specially used for network communication module, if the current data interaction is not through HTTP protocol, you can use NET module, such as WebSocket.

In essence, HTTP protocol transmits data in the form of text, which has a large amount of data, and its transmission requires conversion and parsing between binary and text.

In NodeJS, HTTP modules are inherited from NET modules.

OSI reference model

Open System Interconnect (OSI) stands for Open System interconnection. It is a network interconnection model researched by ISO (International Organization for Standardization) in 1985. The architecture standard defines a seven-layer framework for network interconnection (physical layer, data link layer, network layer, transport layer, session layer, presentation layer, and application layer), which is the OSI Open Systems Interconnection Reference Model. In this framework, the functions of each layer are further specified to achieve interconnection, interoperability and portability of applications in an open system environment.

OSI reference model layering

The OSI reference model can be divided into the following seven layers:

Physical layer → Data link layer → Network layer (IP protocol) → Transport layer (TCP layer) → Session layer → presentation layer → Application layer (HTTP protocol)

The OSI reference model is described as follows:

Layer 1 Physical layer: At the lowest level of the OSI reference model. The main function of the physical layer is to provide the physical connection for the data link layer by using the physical transmission medium, so as to transparently transmit the bit stream. Common devices include network cards, hubs, Repeaters, modems, network cables, twisted pair cables, and coaxial cables.

Layer 2 Data link layer: This layer divides data into frames and handles flow control. Shielding the physical layer, providing the network layer with a data link connection, and almost error-free data transmission (error control) over a physical connection that may go wrong. This layer specifies the topology and provides hardware addressing. Commonly used equipment are network bridge, switch;

Layer 3 Network layer: this layer establishes the connection between two nodes by addressing, selects appropriate routing and switching nodes for packets sent by the transport layer at the source end, and correctly transmits them to the transport layer at the destination end according to the address. It includes routing and relaying data through interconnect networks; In addition to selecting routes, the network layer is responsible for establishing and maintaining connections, controlling congestion on the network, and generating billing information when necessary.

Layer 4 Transport layer: – Regular data delivery – connection-oriented or connectionless. Provide session layer users with an end-to-end reliable, transparent and optimized data transfer service mechanism. Includes full-duplex or half-duplex, flow control, and error recovery services; The transport layer divides messages into groups and reorganizes them at the receiving end. Different packets can be sent to the host over different connections. In this way, high bandwidth can be obtained without affecting the session layer. When establishing a connection, the transport layer can request quality of service (QOS), which specifies acceptable parameters such as bit error rate, latency, security, and end-to-end traffic control.

Layer 5 Session layer: Establishes an end connection between two nodes. Provides a dialogue control mechanism between applications on the end system. This service includes establishing whether the connection is set in full-duplex or half-duplex mode, although duplex mode can be handled in layer 4; The session layer manages the login and logout process. It specifically manages conversations between two users and processes. If only one user is allowed to perform a particular operation at a time, the session-layer protocol manages those operations, such as preventing two users from updating the same set of data in a database at the same time.

Layer 6 Presentation layer: mainly used to deal with the presentation of information exchanged between two communication systems. Solve syntax problems of user information for upper-layer users. It includes data format exchange, data encryption and decryption, data compression and terminal type conversion.

Layer 7 application layer: the highest level in OSI. Provides a means of accessing the OSI environment for specific types of network applications. The application layer determines the nature of communication between processes to meet the needs of users. The application layer not only provides the information exchange and remote operation required by the application process, but also acts as the user agent of the application process to complete some functions necessary for the information exchange. It includes: file transfer access and management PROTOCOL FTAM, virtual terminal VT, transaction processing PROTOCOL TP, remote database access RDA, manufacturing message specification MMS, directory service DS, etc. The application layer can communicate with the application interface to achieve the purpose of presentation to the user. Common protocols include HTTP, HTTPS, FTP, TELNET, SSH, SMTP, POP3, and so on.

OSI reference model each layer function

(1) Physical Layer

The physical layer, the lowest layer of the OSI reference model, uses the transport medium to provide the physical connection to the data link layer.

It is primarily concerned with transmitting bitstreams from one node to another via physical links, which may be copper wires, satellites, microwaves, or other communication media.

The questions it cares about are: How many volts represents one? How many volts represents 0? What is the clock rate? Full-duplex or half-duplex transmission? In general, the physical layer is concerned with the mechanical, electrical, functional, and procedural characteristics of the link.

(2) Data Link Layer

The data link layer provides services for the network layer and solves the communication problems between two adjacent nodes. The transmitted protocol data unit is called data frame.

Data frames contain physical addresses (also called MAC addresses), control codes, data codes, and parity codes. The main function of this layer is to transform unreliable physical links into error-free data links for the network layer by means of verification, validation and feedback retransmission.

In addition, the data link layer also coordinates the data transmission rate of the sender and the receiver, that is, carries out flow control to prevent the receiver from overrunning the buffer and blocking the line because it is too late to process the high-speed data from the sender.

(3) Network Layer

The network layer serves the transport layer and transmits protocol data units called packets or packets.

The main function of this layer is to solve the problem of how to send packets through each node, that is, to the destination through the path selection algorithm (routing).

In addition, to avoid network congestion caused by excessive data packets in the communication subnet, the number of incoming data packets needs to be controlled (congestion control).

When packets need to cross multiple communication subnets to reach their destination, the problem of Internet interconnection needs to be solved.

(4) Transport Layer

The role of the transport layer is to provide reliable and transparent end-to-end data transfer services for the upper layer protocols, including error control and flow control.

This layer hides the details of the lower level data communication from the upper level, so that the upper level user sees only a reliable host-to-host data path that can be controlled and set by the user between the two transport entities.

The transport layer transmits units of protocol data called segments or messages.

(5) Session Layer

The main function of the session layer is to manage and coordinate the communication (dialogue) between various processes on different hosts, that is, to establish, manage and terminate the session between applications. The session layer gets its name because it is similar to the concept of a session between two entities. For example, an interactive user session starts with logging on to the computer and ends with logging out.

(6) Presentation Layer

The presentation layer deals with the representation of the data encoded through the nodes to ensure that information sent by one system application layer can be read by another system application layer.

If necessary, this layer can provide a standard representation for converting a variety of data representations within a computer into a standard representation used in network communications.

Data compression and encryption are also among the transformation capabilities that the presentation layer can provide.

(7) Application Layer

The application layer is the top layer of the OSI reference model and is the interface between the user and the network.

This layer uses applications to fulfill network users’ application requirements, such as file transfer, sending and receiving emails, etc.

TCP

TCP completes the function of the fourth transport layer in the OSI reference model. Net module is simply the Node implementation of TCP protocol.

Transmission Control Protocol (TCP) is a connection-oriented, reliable, byte stream based transport-layer communication Protocol defined in RFC 793 of the IETF.

When the application layer sends an 8-byte data stream to the TCP layer for internetwork transmission, TCP divides the data stream into message segments of appropriate length. The maximum transmission segment size (MSS) is usually limited by the maximum Transmission unit (MTU) at the data link layer of the network to which the computer is connected. TCP then passes the packet to the IP layer, which transmits the packet across the network to the TCP layer of the receiving entity.

To ensure the reliability of packet transmission [1], TCP assigns a serial number to each packet, and the serial number also ensures the sequential reception of packets sent to the receiving entity. The receiving entity then sends back an acknowledgement (ACK) of the successfully received byte; If the sending entity does not receive acknowledgement within a reasonable round trip delay (RTT), the corresponding data (if lost) will be retransmitted.

TCP is reliable, but at the expense of efficiency, and is suitable for scenarios such as transferring files. UDP can be used in scenarios that do not require high reliability but high efficiency, such as live video broadcasting.

UDP

UDP is the User Datagram Protocol. It is a connectionless transport layer Protocol in the Open System Interconnection (OSI) reference model. IETF RFC 768, which provides a transaction-oriented simple unreliable message delivery service, is the official specification for UDP. The protocol number of UDP IP packets is 17.

UDP packets lack reliability assurance, sequence assurance, and traffic control fields, resulting in poor reliability. However, because UDP has few control options, it provides low latency and high data transmission efficiency during data transmission. Therefore, UDP is suitable for applications that do not require high reliability, such as AUDIO, video, and common data, or applications that ensure reliability, such as DNS, TFTP, and SNMP.

Nodejs also provides UDP/Datagram modules that can be called when you need to use UDP.

The resources

Net module: nodejs.org/dist/latest…

UDP/Datagram module: nodejs.org/dist/latest…

OSI reference model: baike.baidu.com/item/OSI%E5…

TCP:baike.baidu.com/item/TCP/33…

UDP:baike.baidu.com/item/UDP