DIY Create a Web Server (non-socket implementation)

Source: Internet
Author: User

Directory

    • Introduced
    • The role of Web server in Web Architecture system
    • Web server interaction with a Web site program
    • HttpListener and socket two different ways
    • Included demo source overview
    • Demo effect
    • Summarize

Introduced

This article focuses on using the HttpListener type to create a Web server yourself, creating a Web server capable of receiving HTTP requests from the browser side and being able to pass to the corresponding Web site for processing. Finally, the processing results (HTML or other formatting) are returned to the browser.

Bloggers have previously described the process of using the socket to simulate Web server operation, and the socket emulation browser to send HTTP requests. See separately:

    • Socket Request Web Server Procedures
    • Socket implement a simple web Server

Instead of using a socket, this article uses an additional, more abstract, System.Net.HTTPListener type implementation.

Web Server in the Web the role of the architecture system

Web server plays a very important role in a B/s architecture system, and Web developers do not need to know much about its working mechanism. Although different Web servers may not be exactly the same, the following three features are almost all Web server must have:

    • Receiving HTTP requests from the browser side
    • Forward the request to the specified Web site program (the latter is written by the web developer responsible for processing the request)
    • Send request processing results to the browser

Displays the important locations where Web server is located throughout the Web Architecture system:

For example, Web server plays a "nexus" role (although not "up and down"), it is responsible for connecting users and Web sites.

We can see that Web server, by default, needs to "communicate" with Web site programs written by web developers (assuming that three sites are on one Web server), so what do we do here? In real time, any web developer using a platform to develop web programs must adhere to certain "rules," such as the use of certain frameworks. Following these rules, the developed web site can be placed on the Web server, is it a bit like "program extension" meaning?

Web Server with the Web Interaction of website Programs

Each site is like a "plug-in", as long as the Web site development process to follow the rules of the Web server, then the site can "plug" on the Web server, we can access the site through a browser.

In theory, each Web server is a host, and each Web site is a plug-in (plug-in). Web server is mainly responsible for communication and other functions, the website program is mainly responsible for data processing.

As for how the host communicates with plug-ins, see an article in front of the blogger, "Application extensions."

Because each Web server can receive HTTP requests continuously, there should be a looping structure similar to the one shown in each Web server:

For example, in order to improve the efficiency of the Web server receiving HTTP requests, the dashed box in the diagram is generally asynchronous, and the request processing does not affect the entire loop.

HTTPListener with the Socket two different ways of difference

In fact, the HTTP protocol is the application-layer protocol. Data is still transmitted over the transport layer using TCP, so it is entirely possible to simulate the Web server's work process by using the socket itself (as mentioned at the beginning of the article). Bloggers in front of a blog about "using the socket Request Web Server" is actually using a socket to simulate the browser's communication behavior. In the System.Net namespace in. NET, there are some higher-level, more abstract types that can also be done to simulate a browser For types such as System.Net.HTTPWebRequest and System.Net.HTTPWebResponse, please refer to the table below for the differences between them and the direct use of sockets:

Classification

Web Server Side

Browser-side

Advantages

Disadvantages

Socket mode

socket.accept :

Responsible for receiving the socket connection request from the browser side

socket.receive :

Responsible for receiving data sent by the browser

Socket.send :

Responsible for sending data to the browser

Socket.connect :

Responsible for sending connection requests to Web server

socket.receive :

Responsible for receiving replies from Web server

Socket.send :

Responsible for sending requests to Web server

More flexibility at the bottom

Lower level, need to fully understand the HTTP protocol, TCP/IP protocol

system.net the type in the namespace

httplistener.getcontext :

 

httplistenerrequest :

 

httplistenerresponse :

HttpWebRequest :

 

HttpWebResponse :

more abstract, usage fixed (but required already included)

As you can see, the two approaches ultimately achieve the same effect.

Note: Please distinguish HttpWebRequest with the ASP . HttpRequest in the . The latter can only be used in ASP . NET , which belongs to the core object in ASP. In the same vein, distinguish between HttpWebResponse and ASP . HttpResponse. Their namespaces are: System.Net and system.web, respectively.

included Demo Source Overview

The source code contains three items, respectively:

    • Httpserver: A simulated web Server (less than 70 lines of code)
    • HttpUtility: An abstraction layer that is specifically designed for interaction between Web server and Web site programs. The dependency inversion principle (DIP) is fully applied here to reduce the degree of coupling between Web server and Web site programs.
    • MyWebsite: A (simulated) Web-based program that relies on HttpUtility.

If we compare the three blocks in the demo with the real one by one analogy, then Httpserver is iis/apache,httputility is the framework/principle we need to use to develop our web programs, and Mywebsite is the Web site program we developed.

Copy the compiled Mywebsite project DLL files to a Web folder in the same directory as the Httpserver executable (similar to the process of publishing a Web site). You can access the Mywebsite Web site in your browser by opening the HTTPServer.exe file to run.

The source code comments more detailed, here is not much to say the source of things.

Demo effect

Summarize

Both implementations of the process, code structure are similar. The main master two points:

    • Loop structure (pump) in Web server, responsible for receiving requests continuously
    • Interaction between Web server and Web site programs (plug-in)

As a web developer, it is also necessary to understand the knowledge that is almost impossible to use.

SOURCE Download: Http://files.cnblogs.com/xiaozhi_5638/HTTP_Web_Server.rar

Forward please keep the original link address.

DIY Create a Web Server (non-socket implementation)

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.