A Web server, also known as an HTTP server, communicates with clients over HTTP. This client usually refers to a web browser.
The working principle of the Web server can be summarized as: the client connects to the server, the client sends a request to the server, the server sends a response to the client, and the client is disconnected from the server. A simple transaction processing event is implemented in this way. It does not seem simple. It is actually quite simple to do ...... It should be noted that the direct communication between the client and the server is not connected, that is, when the server sends a response, it will disconnect from the client and wait for the next request.
HTTP is a protocol that allows web servers and browsers (clients) to send and receive data over the Internet. It is a request and response protocol-the client sends a request and the server responds to the request. HTTP uses a reliable TCP connection, usually using TCP port 80.
In HTTP, the Client Always initiates a transaction by establishing a connection and sending an HTTP request. The server cannot actively contact the client or send a call back connection to the client. Both the client and the server can interrupt a connection in advance. For example, when you use a browser to download an object, you can click "stop" to interrupt the download of the object and close the HTTP connection to the server.
HTTP enables web servers and browsers to exchange data through the Web. It is a request/response protocol, that is, the server waits for and responds to the client's request. H t p does not maintain the connection with the customer, it uses a reliable t c p connection, usually using TCP port 80. The customer/server transmission process can be divided into four basic steps:
1) establish a connection between the browser and the server;
2) the browser requests documents from the server;
3) the server responds to browser requests;
4) disconnect.
H t p is a stateless protocol that does not maintain the connection status information.
1. customer request
The customer request contains the following information:
Request Method
Request Header
Request data
(1) The request method is used by the program HTTP Request Method for a specific u r l or we B page:
Method description
Document specified by the GET request
Head only requests the Document Header
The post request server receives the specified document as executable information.
Put replaces the content in the specified document with the data transmitted from the client.
Delete request server Delete specified page
Options allows the client to view server performance
Trace for testing-allows the client to view the message collection process
(2) the header information is optional and is used to provide other client information to the server.
If the customer uses some method to obtain data (such as p o s t), the data is placed after the header (h e a d e r); otherwise, the client waits
Response from the server.
2. Server Response
Server response includes the following key parts:
Status Code
Response Header
Response Data
HTTP defines multiple sets of status codes returned to the browser.
The Response Header provides the client with the server and/or request document information. HTTP Response Headers:
Method description
Server Web Server Information
Date current date/time
Last modified request document last modified
Expires Request Document expiration time
Content-Length (bytes)
Content-Type data m I m E Type
WWW-authenticate is used to notify the customer of the required authentication information (such as the user name and password)
If there is data requested by the client, the data is placed after the response header; otherwise, the server is disconnected.
3. Communication instance:
1. Request
In this example, the u r L of the browser Request Document is H ttp: // www.hostname.com/index.html. All requests end with empty rows.
GET/index.html HTTP/1.1
Accept: text/plain
Accept: text/html
User-Agent: Mozilla/4.5 (winnt)
(Blank line)
(Data)
The browser uses the get method to request the document/index.html. The browser declares that it can only receive plain text and HTML data, and uses the Mozilla/4.5 (Netscape) engine.
2. Response
The service response includes the status code, header information (ended with a blank line), and request data. If the data exists, the response information is as follows:
HTTP/1.1 200 OK
Date Sunday, 15-jul-05 12:18:33 GMT
Server: Apache/1.3.6
Mime-type: 1.0
Content-Type: Test/html
Last-modified: Thursday, 02-jun-05 20:43:56 GMT
Content-Length: 1432
(Blank line)
<HTML> (this row starts with the data section and needs to be empty with the previous header)
<Head>
<Title> example server-browser communication </title>
</Head>
<Body>
......
</Body>
</Html>
If the document is not found, the response is as follows:
HTTP/1.1 404 not found
Date Sunday, 15-jul-05 12:19:33 GMT
Server: Apache/1.3.6
Note: Use/R/N for each line, and leave multiple empty rows between the header and data segment. That is, use/R/n twice.
In particular, when a requested webpage contains images or videos, the server first sends a text message, disconnects the connection, the client sends the request again, and the server sends the image information again, how many images (except messages that are labeled as text messages) the server will be disconnected/connected to the client several times. Below is a simple web server I wrote in Java:
If you want code, download it by yourself:
Now I want to explain some running problems:
While(True){
System.Out. Println ("the server is waiting ...... ");
// Let the server wait for the connection from the client cyclically. If the whlie (true) Statement is absent
// When disconnected, the web server also exits the program and cannot be used.
Socket Client = server. Accept ();
// Process the client
Processclient (client );
}
The server is waiting ......
(Empty lines here)
The server is waiting ......
2. jpg
The server is waiting ......
3. jpg
The server is waiting ......
1. gif
The server is waiting ......
Sound.pdf
The server is waiting ......
The first "server is waiting ......" It is output after the server is created successfully, and then waits for the client to connect (that is, execute Socket Client = server. accept (); when the code is executed, the program is in the blocking state). When the client is connected, empty lines are output, that is, the request homepage. When the server sends text to the client, disconnected, the server enters the waiting state again. At this time, the second "server is waiting..." Is output ......", The customer machine resends the 1.jpg image file request again, and so on, knowing that all the information of the client request is sent, which is also one of the disadvantages of communication between the Web server and the browser (client, that is to say, HTTP requests can only be completed once without connection. If there are multiple images in the HTML file and each image is transmitted, a connection must be established between the client and the server. Of course, a good web server is not just that simple, but as long as we master their communication principles, writing a good web server is not a difficult task ......