This paper is participating in theNetwork protocols must be known and must be known"Essay campaign
This article as Linux socket(TCP) network programming practice, using C language code to build a simple HTTP server, complete the interaction with the browser, and finally display a picture on the browser; Through this example can consolidate the socket multi-threaded use, but also easy to learn to understand the HTTP protocol.
2. HTTP protocol introduction
HTTP itself is based on THE TCP communication protocol to transfer data (HTML files, image files - also called hypertext transfer protocol), HTTP must work on the client-server architecture (itself is TCP), HTTP default port number 80(browser access default port 80). But you can change it to 8080 or another port (you can specify the port manually).
The HTTP protocol is connectionless, which limits processing to one request per connection; The server disconnects from the customer after processing the request and receiving the reply from the customer. In this way, transmission time can be saved.
3. HTTP message structure
The request message sent by the client to the HTTP server consists of four parts: request line, request header, blank line, and request data
Here is a request from the browser to compare the format of the image above:
GET/HTTP/1.1 Host: 10.0.0.6 Connection: keep-alive upgrade-insecure -Requests: 1 user-agent: Mozilla / 5.0 (Windows NT 10.0; Win64; X64) AppleWebKit (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36 Accept: text/html,application/xhtml+xml,application/xml; Q = 0.9, image/avif, image/webp image/apng, * / *; Q = 0.8, application/signed - exchange; v=b3; Q =0.9 Accept-encoding: gzip, deflate Accept-language: zh-cn,zh; Q = 0.9Copy the code
The common HTTP requests are GET and POST.
HTTP1.0 defines three request methods: GET, POST, and HEAD.
HTTP1.1 adds five new request methods: OPTIONS, PUT, DELETE, TRACE, and CONNECT.
The HTTP server response to the client consists of four parts: the status line, the message header, the blank line, and the response body.
"HTTP / 1.1 200 OK \ r \ n" "Content-type:image/jpeg\r\n" "Content-Length:1234\r\n" "\r\n" "... The body..." Copy the code
Field meanings of the packets listed above:
HTTP/1.0 200 OK: HTTP/1.0 indicates that the protocol is HTTP. 1.0 is the protocol version. 200 means success
Content-type: tells the browser what type of data to send back
Content-length: tells the browser the size of the entity body in the packet, that is, the Length of the returned Content
There are several types of status codes in the above fields, respectively:
200 - Request successful
301 - Resources (web pages, etc.) are permanently transferred to another URL
404 - Requested resource (web page, etc.) does not exist
500 - Internal server error
4. HTTP interaction flow
The first request is made by an HTTP client (browser). The HTTP server receives the request, parses it, and then completes the subsequent interaction.
If you want to display an image on the browser, the interaction flows like this:
To get the browser to display an image in the interface, you have to write an HTML code to the browser that simply uses an image tag.
The HTML code used for the current program is relatively simple and is posted below:
! DOCTYPE HTML html head titlejpg/title meta http-equiv="content-type" content="text/html; charset=iso-8859-1" / /head body centerimg src="www/123.jpg" width="512px" height="384px" / /center /body /html Copy the code
Then you need to prepare a JPG image, as a resource file, easy to pass to the browser, the local file structure is as follows:
5. Case code: Set up HTTP server
The following code responds to a browser request in multithreaded form.
6. The effect of the final operation