day35 --- Network programming basic expansion (theory)

1. Application layer

The origin of the application layer: users use applications, all work in the application layer, the Internet is developed, everyone can develop their own applications, data is diverse, you must specify the data organization 

Example: TCP protocol can transfer data for various programs, such as Email, WWW, FTP, etc. Then, there must be different protocols stipulating the format of emails, web pages, and FTP data, and these application protocols constitute the "application layer".

 2、Socket

We know that if two processes need to communicate, the most basic premise is to be able to uniquely mark a process. In local process communication, we can use PID to uniquely mark a process, but PID is only unique locally, two processes in the network There is a great chance of PID conflict. At this time, we need to find another way. We know that the IP address of the IP layer can uniquely identify the host, and the TCP layer protocol and port number can uniquely identify a process of the host, so we can use the ip address Protocol + port number uniquely identifies a process in the network.

After being able to uniquely identify the processes in the network, they can use sockets to communicate. What is a socket? We often translate sockets into sockets. Sockets are an abstraction layer between the application layer and the transport layer. It abstracts the complex operations of the TCP / IP layer into a few simple interfaces for the supply layer to call the implemented process on the network.中 通信。 Chinese communications.

 

Socket originated in UNIX. Under the philosophy of Unix that everything is a file, socket is an implementation of the "open-read / write-close" mode. The server and the client maintain a "file". Write content to your own file for the other party to read or read the other party's content, and close the file when the communication is over.

 

3. Implementation of network communication

Four basic elements to achieve network communication:

# 1. The IP address of the machine 
# 2. The subnet mask 
# 3. The IP address of the gateway 
# 4. The IP address of the DNS

There are two ways to obtain these four elements: (1) static acquisition (that is, manual configuration); (2) dynamic acquisition (obtained through DHCP)

 

 (1) Ethernet header

The first "Ethernet header" sets the MAC address of the sender (this machine) and the MAC address of the receiver (DHCP server). The former is the MAC address of the local network card, the latter does not know at this time, fill in a broadcast address: FF-FF-FF-FF-FF-FF.

 

(2) IP header

The "IP header" at the back sets the IP address of the sender and the IP address of the receiver. At this time, the machine does not know about both. Therefore, the IP address of the sender is set to 0.0.0.0, and the IP address of the receiver is set to 255.255.255.255.

 

(3) UDP header

The last "UDP header" sets the port of the sender and the port of the receiver. This part is specified by the DHCP protocol. The sender is port 68 and the receiver is port 67.

 

 

After the data packet is constructed, it can be sent out. Ethernet is broadcast, and every computer on the same subnet has received this packet. Because the MAC address of the receiver is FF-FF-FF-FF-FF-FF, you ca n’t see who 
it was sent to, so every computer that receives this packet must also analyze the IP address of this packet to determine Not for yourself. When the IP address of the sender is 0.0.0.0 and the receiver is 255.255.255.255 , the DHCP server knows that
"this packet was sent to me", and other computers can discard this packet. Next, the DHCP server reads the data content of this packet, assigns an IP address, and sends back a "DHCP response" packet. The structure of this response packet is also similar. The MAC address of the Ethernet header is the network card address of both parties.
The IP address of the IP header is the IP address of the DHCP server (sender) and
255.255.255.255 (receiver), UDP The header ports are 67 (sender) and 68 (receiver). The IP address assigned to the requester and the specific parameters of this network
are included in the Data section. The newly joined computer receives this response packet, so it knows its IP address, subnet mask, gateway address, DNS server and other parameters

4. Network communication process

https://www.cnblogs.com/linhaifeng/articles/5937962.html

 

Guess you like

Origin www.cnblogs.com/surpass123/p/12723935.html