2024-07-12
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
In the previous chapter, we introduced the use of Java code to operate files. Today, let's talk about some basic knowledge points of the network so that we can have a deeper understanding of the network in the future.
TCP/IP five-layer network model (important)
如果各位对文章的内容感兴趣的话,请点点小赞,关注一手不迷路,讲解的内容我会搭配我的理解用我自己的话去解释。如果有什么问题的话,欢迎各位评论纠正 🤞🤞🤞
Homepage:N_0050-CSDN Blog
Related columns:java SE_N_0050's Blog - CSDN Blog Java Data Structure_N_0050's Blog-CSDN BlogJava EE_N_0050's Blog - CSDN Blog
The network is divided into Network Principles and Network Programming,Network communication: multiple computers are connected together to complete data sharingIn fact, it is the data transmission between computers through the network, which can also be said to be network data transmission.
A local, locally established private network that enables network communication between hosts within the LAN, also known as intranet; LANs cannot communicate with each other if there is no connection between them.The range of the LAN is fixed, such as a school, a factory,Small coverage, fast transmission efficiency, low latency, mainly used for local resource sharing and communication
By connecting multiple LANs through routers, a large-scale network is formed physically, thus forming a wide area network.. All local area networks within a wide area network belong to its subnet.In fact, it is a group of local area networks connected by routers, which can be regarded as a huge local area network called a wide area network.Large coverage, slow transmission efficiency, and high latency.Mainly used for remote connection and data transmission
If it is a global public wide area network, it is called the Internet (also known as the public network, the extranet), which is a subset of the wide area network. Sometimes in a loose context, the wide area network actually refers to the Internet.
The relationship and difference between LAN and WAN:
For example, a company has many branches in different regions, and each branch can be regarded as a local area network. We can connect all branches through a network connection, so that cross-regional communication can be achieved. This is called a wide area network.
The third layer (network layer) of the OSI model is mainly used to connect multiple networks and select the best path for forwarding according to the destination address of the data packet.
It can be imagined that usually our computer room computers are connected to a router, and only these computers can communicate. However, if the routers are connected to each other, a huge network can be gradually formed, and more computers can communicate. In addition, the router can be connected to the external network, so that we can communicate with computers anywhere.
Routers generally have 5 network ports, 1 WAN and 4 LAN. The WAN port is used to connect to the upper router. The LAN port is inserted into the router for multiple computers to communicate in the same LAN. However, the ports are limited, so the introduction of switches can connect to more devices through the switch. The switch is also connected to the LAN port.
At the second layer (data link layer) of the OSI model, each switch has a MAC address table.(also called address forwarding table),Records the MAC address of the device connected to each portBy querying this table,The switch can determine the forwarding path for each data frame and then forward the data frame to the destination device.
The switch port does not need to be specially configured, just connect it to the LAN port of the router. The switch is used to expand the connection points within the LAN (that is, it can connect more devices). It has more ports (8, 16, 24 or more), can connect more devices, and connect to the same network to facilitate network communication.
The difference between the two:
Routers are at the network layer, and routers are addressed based on IP addresses,The router can handle TCP/IP protocol, the switch cannot,The switch is addressed based on the MAC address. The switch is at the data link layer
The router can assign one IP to many hosts, and these hosts only show one IP to the outside world.The switch can connect many hosts, each of which has its own IP address.
Routers can connect to different types of networks and have wide area network connectivity, so it is suitable for building large enterprise networks or Internet access.Switches are used in internal LANs to connect multiple devices.
example:Each of us is equivalent to a host, the router is equivalent to a courier, the dormitory manager is equivalent to a switch, and the school is a local area network.The courier delivers the package to the school according to the school address (IP), and then hands it over to the dormitory manager according to the dormitory building (subnet IP). The dormitory manager hands it over to you according to your name (MAC).
IP address is used to locate the network address of a device (such as a host, router, etc.), which can be understood as describing the address of our computer on the Internet. The role of IP address in the network:Used for routing and network positioning between devices to ensure that data packets can find the target device on the network. Note that IP addresses can change
Format:In computers, a 32-bit binary number is usually divided into four "8-bit binary numbers" (that is, 4 bytes) to represent the address, but using binary to represent it is inconvenient.IP addresses are usually represented in dotted decimal format, using four decimal numbers between 0 and 255, separated by three dots to represent the IP address.
The IP address locates the address of the host on the network.But after the data is transferred to the host, which program receives it? It needs to be identified by the port number.
The port number is used to distinguish different applications on the host, or to locate the process in the host.。Different programs cannot bind to the same port number, just like mysql's port number is bound to 3306, then if your other programs are also bound to this port number, it will be a mess.But a program can bind multiple port numbers
Format:The system port number range is 0 ~ 65535 (2 bytes), 0 ~ 1023 is generally used for special purposes in the system and we generally do not use it
You can think of IP and port number as a pair, similar to sending a courier. There are sender address (source IP), sender phone number (source port number), recipient address (destination IP), recipient phone number (destination port number), and protocol (courier company). I will explain it in detail later.
With the IP address and port number, you can locate the only process in the network, but network communication uses binary data for transmission.You need to tell the other party what the data is. At this time, you need to use a protocol to specify the data format of both parties.
The network essentially transmits data through optical/electrical signals. We use protocols to specify the data format of both parties. Protocols are agreements, similar to gang transactions. They need to agree to bring what both parties want before the transaction. Otherwise, it will be a swishing deal.
Quintuple:
In the TCP/IP protocol, a five-tuple is used to identify a network communication. The following is the necessary information in a network communication:
1. Source IP: identifies the source host
2. Source port number: identifies the process in the source host that sends data for this communication
3. Destination IP: identifies the destination host
4. Destination port number: identifies the process in the destination host that receives data for this communication
5. Protocol number: identifies the data format agreed upon by the sending process and the receiving process
The source port number and destination port number are at the transport layer, the source IP and destination IP are at the network layer!!!
I don't understand the picture below.
For network protocols,It is divided into several levels for definition and management. According to the type and function of the protocol, the hierarchical relationship is set. The upper layer calls the lower layer, and the lower layer provides support to the upper layer, which is similar to the meaning of calling an interface.The advantage is that when we call the interface, we don't need to know how it is implemented at the bottom. We just need to know how to use the interface and what it does. The interface doesn't need to know who is calling it and it will encapsulate the implementation details to avoid modification.
Layered example:Just like a big company, the boss issues an order, similar to recursion, and passes it down. Leaders at each level receive the order, pass it to the employees for processing, and then return it after processing, and finally return it to the boss for a response. Communication in this way is convenient for management and overall order.
The OSI seven-layer model is complex and impractical, so the OSI seven-layer model has not been implemented. It only appears in books for us to understand. Most people still use the TCP/IP five-layer network model.
It is a simplified version of the OSI seven-layer network model, combining the application layer, presentation layer, and session layer in OSI into one application layer.The TCP/IP communication protocol uses a 5-layer hierarchical structure.Each layer calls the network provided by the next layer to complete its needs
Here are some examples to illustrate each layer:For example, if we buy a piece of clothing on Taobao, you can also choose to buy other items such as pants, which belongs to the application layer. Then the merchant receives information related to our order, such as the recipient, phone number, etc. This belongs to the transport layer. Then the merchant calls a courier company to handle it, and the courier company chooses the appropriate route (considering many factors such as efficiency and cost), such as Shanghai to Hangzhou to Shaoxing for delivery, which belongs to the network layer. Then we need to consider how to deliver to adjacent places, such as Shanghai to Hangzhou by rail, Hangzhou to Shaoxing by plane, which belongs to the data link layer. Finally, transportation equipment is used for delivery, which belongs to the physical layer.The following encapsulation and division will be explained with examples.
Physical layer:The data link layer converts the data frame into binary numbers, transmitted through physical media (such as cables, optical fibers, etc.)
Data link layer: responsible forData frames are transmitted on the same LAN,Focusing on the details of transmission between adjacent nodes, switches are at the layer
Network layer: responsible forTransmitting data packets between different networks, and choose the best path,The focus is on route planning in communications, and the router is at the layer
Transport layer: provides end-to-endCommunication service, ensuring complete data transmission.Focus on where communications start and end
Application layer: interacts directly with applications and provides network services
The layer where the network device is located:
forA hostFor example, its operating system kernel implementsTransport layer to physical layerContent
forA routerIn other words, it realizesNetwork layer to physical layer
forA switchIn other words, it realizesData link layer to physical layer
forA hubIn other words, it realizesPhysical Layer
Describe the process of data transmission during network communication. Let's take an example of sending information. First, let's talk about encapsulation. In fact, it is similar to sending express delivery.
sender
Application layer:
We use the chat program to send a message to someone, and wx will package the java we input into an application layer data packet(Only wx programmers know the format) Use strings for splicing, just like when we send a courier, we pack the item ourselves, and then the courier (transport layer) will pack it again.The application layer protocol can be flexibly adjusted by programmers
Then the application layer data packets are transmitted to the transport layer for processing through the operating system's API
Transport layer:
Transport layer protocols such as TCP, UDP. Pack the data packets of the application layer again, that is, use the string to splice the packet header of the transport layer, such as TCP and UDP, to become a data packet belonging to the transport layer. A data packet = header + payload
At this time, the data packet of the transport layer = UDP header + the data packet of the subsequent application layer (that is, the payload of the packet header). The UDP header is a binary data containing the key information of the source port number and the destination port number., and then it is packed. For example, when sending the display screen, put a label on it as a fragile item and add some protection measures to reduce the possibility of breakage.
Remember the source and destination port numbers are at the transport layer!The data packets at the transport layer are processed and then passed to the network layer for processing
Network layer:
The most important protocol of the network layer is the IP protocolThe network layer also packages the data packets of the transport layer and uses a string to splice the IP header to become a data packet belonging to the network layer.
At this time, the network layer data packet = IP header + UDP header + application layer data packet (that is, the payload of the IP header). The IP header contains many attributes, the most important of which are the source IP and destination IP.
Remember source IP and destination IP are at the network layer!The data packets at the network layer are processed and then passed to the data link layer for processing
data link layer:
The typical protocol of the data link layer is Ethernet, where Ethernet is used to represent the medium for network data transmission.The data link layer will repackage the network layer data packet, use the string to splice the Ethernet frame header and add a frame tail, and turn it into a data link layer data packet.
At this time, the data link layer data packet = Ethernet frame header + IP header + UDP header + application layer data packet (that is, the payload of the Ethernet header) + frame tail. The Ethernet header also contains many attributes, the most important of which are the source MAC address and the destination MAC address (which describes the address of the network card/network adapter).
What is a network card/network adapter? A piece of computer hardware designed to allow computers to communicate on a computer network., it has a fixed address when it is manufactured.We generally use MAC addresses to identify network cards/network adapters。In a local area network, devices communicate through MAC addresses to ensure that data frames can find the correct network card.
Then the data link layer data packet is processed and then passed to the physical layer for processing
Physical layer:
Convert the above Ethernet data frame, binary structure data such as 0 and 1, into optical/electrical signals for transmissionFrom the beginning of the upper layer protocol to the lower layer protocol, each layer is added with a header, which is actually encapsulation.
The transmission process encounters a router:After the data is transmitted, it is forwarded through switches and routers. It can be imagined that our computers can communicate because there are a bunch of switches and routers on them.Please note that when passing through a router, the physical layer receives the photoelectric signal and converts it into binary data and hands it over to the data link layer. The data link layer does the same operation, parses it, extracts the payload part and hands it over to the network layer. The network layer then parses it and selects the best path to forward it. It then encapsulates it to the data link layer and then to the physical layer for transmission. If it encounters a router again, the operation is the same.
The transmission process encounters a switch:But if it passes through a switch, it is the same.The physical layer converts the photoelectric signal into binary and passes it to the data link layer. The data link layer then parses and extracts the payload part. Based on the information in the payload part, it decides where to send the data next. It then creates a new packet header and packet trailer and encapsulates it and passes it to the physical layer. The physical layer then transmits it. When the data reaches the receiver, it needs to be parsed (divided), which is to unpack the data.
receiver
Physical layer:receivedThe photoelectric signal from the sender is converted into binary data and the Ethernet frame header is obtained., handed over to the data link layer for processing
data link layer:Using Ethernet protocolParse the Ethernet data packet and take out the packet header and packet tail, and pass the payload to the network layer for processing
Network layer:Use IP protocol to parse the data packets at the network layer.Take out the IP packet header and parse it, and then throw the payload part to the transport layer for processing
Transport Layer: Use the UDP protocol to parse the data packets at the transport layer.Take out the UDP packet header and parse it, and then pass the payload to the application layer for processing
Application layer:The data from the transport layerAccording to the port number, it is thrown to the corresponding programThen the wx program parses the data here. How it is parsed is based on the application layer protocol customized by the programmer.
Summary of the whole process:Messages you sentAfter layers of encapsulation, it is converted into physical signals (such as optical/electrical signals, etc.) for transmission, first of all,The current LAN makes a judgment and then goes to the local router,The local router will encapsulate and select the appropriate path, and then on the wayWhen encountering other routers, they will encapsulate and select the appropriate path until they get close to the target network.. It will be packaged and reused at the final router.Resolve IP address to MAC address through APR protocolIf no corresponding MAC address is found, the router will send a broadcast request.The target device will respond by providing the MAC address, and then the transmission within the LAN will eventually reach the target device.
The above are some basic knowledge points of the Internet. The content of the Internet is very extensive and will be explained in depth later.💕