Node.js network communication (1) - related concepts of network communication, seven-layer network model, ip address of mac address, Port port number, domain name & TCP-Transmission Control Protocol, UDP-User Datagram Protocol, Socket socket
Node.js network communication
Node is a network-oriented platform. It has the characteristics of event-driven, non-blocking, single-threaded, etc., and has good scalability, making it very lightweight and suitable for playing various roles in distributed networks. At the same time, the API provided by Node is very suitable for the network, which is suitable for building flexible network services with its basic API. The content of this course is to introduce Node's specific capabilities in network communication programming.
Using Node can be very convenient to build a network server. In the Web field, most programming languages need a special Web server as a container, such as ASP and ASP.NET need IIS as a server, PHP needs to run in Apache or Nginx environment, etc., JSP needs Tomcat server and so on. But with Node, it only takes a few lines of code to build a server without the need for an additional container.
Node provides four modules: net, dgram, http, and https, which are used to process TCP, UDP, HTTP, and HTTPS respectively, and are suitable for both server and client.
Chapter 1 Network Communication Related Concepts
We use the internet every day, have you ever wondered how it works?
Billions of computers all over the world are connected together and communicate in pairs. A network card in Shanghai sent a signal, and another network card in Los Angeles actually received it. The two actually didn’t know the physical location of the other. Don’t you think this is a very miraculous thing?
The core of the Internet is a series of protocols, collectively referred to as "Internet Protocol" (Internet Protocol Suite). They make detailed regulations on how computers are connected and networked. By understanding these protocols, one understands the principles of the Internet.
Seven-Layer Network Model
The implementation of the Internet is divided into several layers. Each floor has its own function, and like a building, each floor is supported by the floor below.
What the user comes into contact with is only the top layer, and does not feel the lower layer at all. To understand the Internet, we must start from the bottom layer and understand the functions of each layer from the bottom up.
There are different models on how to layer, and some models are divided into seven layers
For better understanding, some models are divided into five layers
The lower layer is closer to the hardware; the upper layer is closer to the user.
mac address
If ping 127.0.0.1 can be pinged, it means that there is no problem with the network card of the computer, and the Internet can be accessed when the network is normal.
IP address
The identification number of a computer in the network is like everyone's phone number.
The role of the ip address: Find the corresponding device in the network through the ip address, and then you can send data to this device
The ip address type is divided into: ipv4, ipv6
Port port number
Port numbers are divided into well-known port numbers and dynamic port numbers (well-known port numbers are used by the system, and dynamic port numbers are set by programmers)
Well-known port numbers: range from0-1023
Dynamic port range: 1024-65535
, when the program is closed, the occupied port number is released at the same time
Check the port number:netstat -an
Check which program the port number is occupied by: lsof -i[tcp/udp]:端口号
(If you can’t find it, use administrator privileges and add sudo)
Kill the specified process according to the process number:kill -9 进程号
domain name
It is convenient to remember the host address of a certain computer, and the domain name can be resolved to an ip address (DNS resolution)
TCP and UDP
TCP and UDP: Both are protocols for data transmission. For example, if I want to give you money, should I give it to you by hand or send it to you by express.
TCP (Transmission Control Protocol)
- It is necessary to establish a connection (three-way handshake) to form a transmission channel in order to transmit data
- Unlimited size of transferred data
- It is a safe and reliable protocol, but the speed is slightly slower
UDP (User Datagram Protocol)
Concept: English Quanpin (User Datagram Protocol) referred to as User Datagram Protocol, it is a connectionless, unreliable network transmission protocol
Core features : no connection, low resource overhead, fast transmission speed, and the maximum UDP data packet is 64K
Advantages : no connection required, fast transmission speed, low resource overhead
Disadvantages : Unreliable data transmission, easy to lose data packets, no flow control, when the other party does not receive data in time, the sender keeps sending data, which will cause the buffer to be full and the computer to freeze, so the receiver needs to receive data in time
- There is no need to establish a connection, and the data is encapsulated into a packet and thrown to the opposite side
- Each packet size is limited to 64K
- Because the connection is not established, the other party may or may not receive the data (packet loss), so it is an unsafe protocol, but the speed is relatively fast
When to use TCP and when to use UDP?
- Use UDP when the speed requirement is relatively high, such as video chat, QQ chat
- Use TCP when data security requirements are relatively high, such as data transmission, file download
- For video chat, if the image quality is the priority, then choose TCP, if the fluency is the priority, then choose UDP
What is Socket
Socket is used for communication and data transmission between two different clients
. If it is transported to the car, then there needs to be a port on the car side, and a port on the fuel dispenser side, and a socket connector (such as an adapter) is added to the ports on both sides, and then a pipeline is connected in the middle to deliver oil. I think Socket The role of the socket is this socket.
Simply put, if you want to transfer data between two clients, then each of the two clients must have a Socket.