httpserver--principle analysis of the project in the "C + + Institute" (27)

Source: Internet
Author: User
Tags epoll

similarities and differences of 1.BS/CS structure

BS is the service-side + browser structure of the network program, for manufacturers, only need to provide the server, are done through the HTTP protocol.

Advantages: There is no need to maintain client software, users do not have to install any third-party software when using, only need to have a browser. Regardless of any operating system (Windows,linux,ios)
Cons: The browser has limited functionality, cannot do overly complex interfaces, and if some features need to be done on the client, the browser may not be able to implement them.
CS is the server-side + client software structure of the network program. For the manufacturer, both the server and client software need to be provided.
Advantage: The client function can be any extension according to the actual requirement, can realize the function that the browser can't do. The interface can also be very beautiful.
Cons: Customers need to install client software, and manufacturers need to support and upgrade the maintenance of customer software, if you want to implement support for multiple operating systems, then you need to provide multiple versions of the client software. Therefore, the workload of maintaining the client will be very large.


From a commercial point of view, BS to bundle users, the general means is to set the browser default home page. CS, want to do everything possible to let users install your software, installed automatically after the implementation.

2.http Server Connection Database design Essentials



For DB, the maximum consumption is not the execution of the SELECT statement, and the maximum consumption is the connection and disconnection.


3.HTTP Request Answer step


HTTP is a short connection based on the TCP protocol.

The first step, the client connects to the server side via the TCP protocol
The second step, the client sends a request to the server requests (client a connection only to the server to send a request)
The third step, server to the client reply reponse.
Fourth step, both sides disconnected
(Client must connect to server again if it wants to send request to server again)


The simplest model for an HTTP request
1. The client sends an HTTP request with a file name followed by a get
2, the server will be the client get files issued down


structure of the 4.HTTP protocol

HTTP request (String)
360 browser
get/http/1.1
Accept:text/html, Application/xhtml+xml, */*
Accept-language:zh-cn
user-agent:mozilla/5.0 (Windows NT 6.1; trident/7.0; rv:11.0) Like Gecko
Accept-encoding:gzip, deflate
host:192.168.27.254:8080
Connection:keep-alive
What the IE sends
get/http/1.1
Accept:text/html, Application/xhtml+xml, */*
Accept-language:zh-cn
user-agent:mozilla/5.0 (Windows NT 6.1; trident/7.0; rv:11.0) Like Gecko
Accept-encoding:gzip, deflate
host:192.168.27.254:8080
Connection:keep-alive

HTTP replyMessage header (String)
http/1.0 OK
Content-type:image/x-icon (identifies what type of message body to reply to the client, the body of the message is the clothes image file, the image is the icon format)
Transfer-encoding:chunked
Connection:keep-alive
Accept-ranges:bytes
content-length:2550 (length of message body, units: bytes)
\ nthe carriage return to newline.
Message body (binary)
File binary stream specifically to be sent to the client
End of Message (string)
\ n
\ n


For example: Enter Http://serverid/in the browser
get/http/1.1
Enter Http://serverid/abc.txt in the browser
Get/abc.txt http/1.1

In the GET request for HTTP s?wd=, s represents the value of the Action property of the form in HTML
WD represents the value of the input Name property, which is followed by the value of the input Value property.
In the HTTP protocol, if the transmission is a Chinese character, then escaped, the original encoding of the Chinese characters into bytes, in front of each byte plus%, followed by 16 binary notation of the string, such as "Preach wisdom" after the conversion,%E4%BC%A0%E6%99%BA, for the English alphabet, not escaped. If the% itself is encountered, the% is escaped to%25 (25 for the ASCII code of%)


5.HTML Introduction

If the message body received by the browser is an HTML-formatted text file, the browser will display the HTML file.
If the message body received by the browser is an image file, then the browser will display the image.
If the browser receives a binary stream file, the browser will download the file.

HTML is a text file written in a special format, and the main purpose of HTML is to describe what a Web page looks like, not a programming language in the usual sense.
<> tags, for HTML tags is paired with the use of </>

<title> is the header
Images
<body>html Body Label
<A> hyperlink tag, where the href attribute represents the domain name or IP address that the hyperlink points to
<a href= "http://www.123.com" > go to 123 this site </A>
<p> segment, attribute align represents the position of the segment in the screen
<form> tags, you can implement some controls
<input> represents a control, type= "text", which represents a lineedit,type= "submit" represents a button, value equivalent to the Text property of the control in Qt.
<meta http-equiv= "Content-type" content= "Text/html;charset=utf8" >


6. Blocking socket multithreading concurrent programming

Because HTPP are based on short connections, it is not possible to have a large number of client connections on the server side, so the use of multi-threading, does not lead to a process with a large number of threads at the same time, blocked socket multithreading can also increase the speed of each client connection response.
For multi-threaded concurrency, even if one of the connections consumes a significant amount of time, it does not affect other connections.

For the Epoll model, the biggest overhead is not epoll_wait, but epool_ctl. So in the context of HTPP this short connection, epoll suitable for a large number of customers will be connected to the server, are long-connected, not often disconnected or connected, and for each connection will not do a lot of data transmission.


In the case of a blocking multithreaded concurrency design, when the main thread does not care about the exit state of a specific child thread, all threads can use the separable state. Let these threads fend for themselves.



7. Project Experience

When creating a thread, the stack address of a function cannot be passed as a parameter of the thread entry function.


Write the server program, to make the best use of servers resources to speed up the processing efficiency of the program, including CPU resources, memory resources.


If you want to assign space to a parameter inside a function, then this parameter must be a level 2 pointer.
For HTTP server, the basic principle is to quickly handle the connection from the client, as long as the customer issued a reponse, immediately disconnect.
Note the place:
For HTTP, the default port is 80 port, but the Linux normal user is not authorized on the 80 port bind, only root has the privilege bind 80 port. And note the firewall settings.

httpserver--principle analysis of the project in the "C + + Institute" (27)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.