network programming
What is web programming:
It is the programming used to exchange data between different computer running programs that realize network intercommunication
network model
OSI (model): seven-layer protocol
Application layer: For example: HTTP protocol mainly solves how to package data, the application program used for communication and the underlying network used for message transmission provide interfaces, and provide common network application services |
---|
Presentation layer: Linux sends packages to windows, and the syntax of the two systems is inconsistent. Just like installation packages, exe cannot be used under linux, and the shell cannot be directly run under windows. So the presentation layer is needed to help us solve the communication grammar problem between different systems |
Session layer: To establish an automatic sending and receiving packet, automatic addressing function. The role of the session layer is to establish and manage communication between applications, and automatically call TCP to package |
Transport layer: For example: The TCP protocol guarantees the accuracy when transferring a large number of files. Therefore, the sent data is encapsulated. Just like sending express delivery, send them one by one. |
Network layer: For example, the IP protocol is the function implemented by routers, switches, and devices with addressing functions. This layer defines the IP address, which is addressed by the IP address. So the IP protocol was born |
Data Link Layer: Transmission through radio waves, through other media. Then I also need to ensure that the bit stream transmitted in the past is correct and has an error correction function. |
Physical layer: mainly defines the physical equipment standard, such as the interface type of the network cable, the interface type of the optical fiber, the transmission rate of various transmission media, etc. Its main function is to transmit the bit stream, |
TCP five-layer model
The application layer in the TCP/IP protocol handles the functions of the fifth layer, the sixth layer and the seventh layer in the seven-layer model. The transport layer in the TCP/IP protocol cannot always guarantee reliable transmission of data packets at the transport layer, while the seven-layer model can. The TCP/IP protocol also offers an alternative called UDP (User Datagram Protocol). UDP does not guarantee reliable packet delivery
HTTP (Hypertext Transfer Protocol)
It is a stateless, application layer protocol based on request and response mode, often based on TCP connection mode
HTTP request
It consists of three parts: request line, message header, and request body.
request line
Method Request-URI HTTP-Version (three parts separated by spaces)
in:
Method indicates the request method GET Request to obtain the resource identified by Request-URI POST Add new data after the resource identified by Request-URI (GET requests often put self-made data in quary_string, POST requests often put custom data in the body Middle) HEAD Request to obtain the response message header of the resource identified by Request-URI PUT Request the server to store a resource and use Request-URI as its identifier DELETE Request the server to delete the resource identified by Request-URI TRACE Request the server to send back the received request Information, mainly used for testing or diagnosing CONNECT reserved for future use of OPTIONS requests to query the performance of the server, or to query resource-related options and requirements Request -URI is a uniform resource identifier; the address of the request HTTP-Version indicates the requested HTTP protocol Version. Now most of them are 1.1
Message header (header)
Several key-value pairs, each key-value pair occupies one line, use between each key and value: split
request body
Generally, GET requests do not have a body, while POST requests do.
HTTP response format
The HTTP response is also composed of three parts, namely: status line, message header, and response body.
status line
HTTP-Version indicates the version of the server HTTP protocol;
Status-Code indicates the response status code sent back by the server;
Reason-Phrase represents a textual description of the status code.
Status code: 1xx: Instructions – Indicates that the request has been received and continues processing; 2xx: Success – Indicates that the request has been successfully received, understood, and accepted; 3xx: Redirection – further operations must be performed to complete the request; 4xx: Client Error – the request has syntax errors or the request cannot be fulfilled; 5xx: Server-side error – the server failed to fulfill a legitimate request.
Message header (header)
It is still a set of key-value pairs, and each key-value pair occupies one line between each key and value: split
Content-Type: Describes the data format type of the body; Content-Length: Describes the data length of the body (Byte); Host: Describes the host name (domain name/ip) accessed; Referer: Which page is the current page from Jumped over; Cookie: string, one of the browser's local storage capabilities, often contains information called "identity identifier" (session id); Session: a data structure maintained by the server, which records The user's identity information, session id is the unique identity of the session object, and the session id is stored in the browser. When the browser subsequently accesses the server, it will automatically carry the session id, so that the server knows which user sent the current request of.
User-Agent: It will tell the website server what tool the visitor uses to request, whether it is a mobile phone or a computer, etc.
Response body (body)
There are also many types of text formats in the response, for example, it can be an html, a css, a javascript, an image data, or a json data
Commonly used commands (emphasis)
ping ,ipconfig ,telnet
Local machine address (emphasis)
127.0.0.1 represents your own computer host
localhost represents this machine
port
The logical address used to identify the process, different process ports are different.
netstat -ano
For example, QQ, WeChat, send data to the computer of the corresponding friend through the ip: port.
QQ port 10.1.1.2:1010
WeChat port 10.1.1.2:10080
TCP and UDP (emphasis)
Two common protocols:
UDP: Encapsulates the data source and destination into data packets, no need to establish a connection
There is a limit on the size of each packet
Because there is no need to establish a connection, the security is poor, but the speed is fast
Application scenario:
game
TCP: Establish a connection to form a channel for transmitting data, and then transmit data through a three-way handshake. High security, low efficiency
Socket
Socket is a mechanism provided by network programming. There must be sockets at both ends of the communication. Network communication is actually communication between sockets. Data is communicated on the io stream between sockets.
TCP communication
Server
client