search
HomeBackend DevelopmentC#.Net TutorialC# implements the simplest HTTP server

C# implements the simplest HTTP server

Nov 24, 2016 am 11:48 AM
c#httpserver

Introduction

This article uses C# to implement the simplest HTTP server class. You can embed it into your own project, or you can read the code to learn about the HTTP protocol.

Background

High-performance WEB applications are generally installed on powerful WEB servers, such as IIS, Apache, and Tomcat. However, HTML is a very flexible UI markup language, which means that any application and back-end service can provide HTML generation support. In this small example, servers like IIS and Apache consume too many resources. We need to implement a simple HTTP server ourselves and embed it into our application to handle WEB requests. We only need one class to implement it, it's very simple.

 Code Implementation

 First let’s review how to use classes, and then we’ll analyze the specific details of the implementation. Here we create a class that inherits from HttpServer and implements the two abstract methods handleGETRequest and handlePOSTRequest:

public class MyHttpServer : HttpServer {
    public MyHttpServer(int port)
        : base(port) {
    }
    public override void handleGETRequest(HttpProcessor p) {
        Console.WriteLine("request: {0}", p.http_url);
        p.writeSuccess();
        p.outputStream.WriteLine("<html><body><h1 id="test-nbsp-server">test server</h1>");
        p.outputStream.WriteLine("Current Time: " + DateTime.Now.ToString());
        p.outputStream.WriteLine("url : {0}", p.http_url);
 
        p.outputStream.WriteLine("<form method=post action=/form>");
        p.outputStream.WriteLine("<input type=text name=foo value=foovalue>");
        p.outputStream.WriteLine("<input type=submit name=bar value=barvalue>");
        p.outputStream.WriteLine("</form>");
    }
 
    public override void handlePOSTRequest(HttpProcessor p, StreamReader inputData) {
        Console.WriteLine("POST request: {0}", p.http_url);
        string data = inputData.ReadToEnd();
 
        p.outputStream.WriteLine("<html><body><h1 id="test-nbsp-server">test server</h1>");
        p.outputStream.WriteLine("<a href=/test>return</a><p>");
        p.outputStream.WriteLine("postbody: <pre class="brush:php;toolbar:false">{0}
", data);     } }

When starting to process a simple request, we need to start a separate thread to listen to a port, such as port 8080 :

HttpServer httpServer = new MyHttpServer(8080);
Thread thread = new Thread(new ThreadStart(httpServer.listen));
thread.Start();

If you compile and run this project, you will see the sample content generated on the page under the browser http://localhost:8080 address. Let's take a brief look at how this HTTP server engine is implemented.

 This WEB server consists of two components. One is the HttpServer class that is responsible for starting TcpListener to listen to the specified port, and uses the AcceptTcpClient() method to process TCP connection requests in a loop, which is the first step in processing TCP connections. Then the request arrives at the "specified" port, and a new pair of ports is created to initialize the TCP connection from the client to the server. This pair of ports is the session of TcpClient, so that our main port can continue to receive new connection requests. From the code below, we can see that each time the listener creates a new TcpClien, the HttpServer class creates a new HttpProcessor, and then starts a thread to operate. The HttpServer class also contains two abstract methods, which you must implement.

public abstract class HttpServer {
 
    protected int port;
    TcpListener listener;
    bool is_active = true;
 
    public HttpServer(int port) {
        this.port = port;
    }
 
    public void listen() {
        listener = new TcpListener(port);
        listener.Start();
        while (is_active) {               
            TcpClient s = listener.AcceptTcpClient();
            HttpProcessor processor = new HttpProcessor(s, this);
            Thread thread = new Thread(new ThreadStart(processor.process));
            thread.Start();
            Thread.Sleep(1);
        }
    }
 
    public abstract void handleGETRequest(HttpProcessor p);
    public abstract void handlePOSTRequest(HttpProcessor p, StreamReader inputData);
}

In this way, a new tcp connection is processed by HttpProcessor in its own thread. The job of HttpProcessor is to correctly parse the HTTP header and control the correctly implemented abstract method. Let's take a look at the processing of HTTP headers. The first line of code for the HTTP request is as follows:

GET /myurl HTTP/1.0

After setting the input and output of process(), HttpProcessor will call the parseRequest() method.

public void parseRequest() {
    String request = inputStream.ReadLine();
    string[] tokens = request.Split(&#39; &#39;);
    if (tokens.Length != 3) {
        throw new Exception("invalid http request line");
    }
    http_method = tokens[0].ToUpper();
    http_url = tokens[1];
    http_protocol_versionstring = tokens[2];
 
    Console.WriteLine("starting: " + request);
}

HTTP requests consist of 3 parts, so we only need to use the string.Split() method to split them into 3 parts. The next step is to receive and parse the HTTP header information from the client, each line in the header information The data is saved in the form of Key-Value (key-value). A blank line indicates the end of the HTTP header information. We use the readHeaders method in our code to read the HTTP header information:

public void readHeaders() {
    Console.WriteLine("readHeaders()");
    String line;
    while ((line = inputStream.ReadLine()) != null) {
        if (line.Equals("")) {
            Console.WriteLine("got headers");
            return;
        }
 
        int separator = line.IndexOf(&#39;:&#39;);
        if (separator == -1) {
            throw new Exception("invalid http header line: " + line);
        }
        String name = line.Substring(0, separator);
        int pos = separator + 1;
        while ((pos < line.Length) && (line[pos] == &#39; &#39;)) {
            pos++; // 过滤掉所有空格
        }
 
        string value = line.Substring(pos, line.Length - pos);
        Console.WriteLine("header: {0}:{1}",name,value);
        httpHeaders[name] = value;
    }
}

So far, we have learned how to handle simple GET and POST requests are assigned to the correct handler respectively. In this example, there is a thorny problem that needs to be dealt with when sending data, that is, the request header information contains the length information of the sent data, content-length. When we hope that the handlePOSTRequest method in the subclass HttpServer can handle the data correctly, we The data length content-length information needs to be put into the data stream together, otherwise the sender will be blocked waiting for data that will never arrive. We use a less elegant but very effective way to handle this situation, which is to read the data into a MemoryStream before sending it to the POST processing method. This approach is less than ideal for the following reasons: if the data being sent is large, or even uploading a file, then it is not appropriate or even possible for us to cache the data in memory. The ideal method is to limit the length of the post. For example, we can limit the data length to 10MB.

Another simplification of this simple version of the HTTP server is the return value of content-type. In the HTTP protocol, the server always sends the MIME-Type of the data to the client to tell the client what type of data it needs to receive. In the writeSuccess() method, we see that the server always sends text/html type. If you need to add other types, you can extend this method.


Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Developing with C# .NET: A Practical Guide and ExamplesDeveloping with C# .NET: A Practical Guide and ExamplesMay 12, 2025 am 12:16 AM

C# and .NET provide powerful features and an efficient development environment. 1) C# is a modern, object-oriented programming language that combines the power of C and the simplicity of Java. 2) The .NET framework is a platform for building and running applications, supporting multiple programming languages. 3) Classes and objects in C# are the core of object-oriented programming. Classes define data and behaviors, and objects are instances of classes. 4) The garbage collection mechanism of .NET automatically manages memory to simplify the work of developers. 5) C# and .NET provide powerful file operation functions, supporting synchronous and asynchronous programming. 6) Common errors can be solved through debugger, logging and exception handling. 7) Performance optimization and best practices include using StringBuild

C# .NET: Understanding the Microsoft .NET FrameworkC# .NET: Understanding the Microsoft .NET FrameworkMay 11, 2025 am 12:17 AM

.NETFramework is a cross-language, cross-platform development platform that provides a consistent programming model and a powerful runtime environment. 1) It consists of CLR and FCL, which manages memory and threads, and FCL provides pre-built functions. 2) Examples of usage include reading files and LINQ queries. 3) Common errors involve unhandled exceptions and memory leaks, and need to be resolved using debugging tools. 4) Performance optimization can be achieved through asynchronous programming and caching, and maintaining code readability and maintainability is the key.

The Longevity of C# .NET: Reasons for its Enduring PopularityThe Longevity of C# .NET: Reasons for its Enduring PopularityMay 10, 2025 am 12:12 AM

Reasons for C#.NET to remain lasting attractive include its excellent performance, rich ecosystem, strong community support and cross-platform development capabilities. 1) Excellent performance and is suitable for enterprise-level application and game development; 2) The .NET framework provides a wide range of class libraries and tools to support a variety of development fields; 3) It has an active developer community and rich learning resources; 4) .NETCore realizes cross-platform development and expands application scenarios.

Mastering C# .NET Design Patterns: From Singleton to Dependency InjectionMastering C# .NET Design Patterns: From Singleton to Dependency InjectionMay 09, 2025 am 12:15 AM

Design patterns in C#.NET include Singleton patterns and dependency injection. 1.Singleton mode ensures that there is only one instance of the class, which is suitable for scenarios where global access points are required, but attention should be paid to thread safety and abuse issues. 2. Dependency injection improves code flexibility and testability by injecting dependencies. It is often used for constructor injection, but it is necessary to avoid excessive use to increase complexity.

C# .NET in the Modern World: Applications and IndustriesC# .NET in the Modern World: Applications and IndustriesMay 08, 2025 am 12:08 AM

C#.NET is widely used in the modern world in the fields of game development, financial services, the Internet of Things and cloud computing. 1) In game development, use C# to program through the Unity engine. 2) In the field of financial services, C#.NET is used to develop high-performance trading systems and data analysis tools. 3) In terms of IoT and cloud computing, C#.NET provides support through Azure services to develop device control logic and data processing.

C# .NET Framework vs. .NET Core/5/6: What's the Difference?C# .NET Framework vs. .NET Core/5/6: What's the Difference?May 07, 2025 am 12:06 AM

.NETFrameworkisWindows-centric,while.NETCore/5/6supportscross-platformdevelopment.1).NETFramework,since2002,isidealforWindowsapplicationsbutlimitedincross-platformcapabilities.2).NETCore,from2016,anditsevolutions(.NET5/6)offerbetterperformance,cross-

The Community of C# .NET Developers: Resources and SupportThe Community of C# .NET Developers: Resources and SupportMay 06, 2025 am 12:11 AM

The C#.NET developer community provides rich resources and support, including: 1. Microsoft's official documents, 2. Community forums such as StackOverflow and Reddit, and 3. Open source projects on GitHub. These resources help developers improve their programming skills from basic learning to advanced applications.

The C# .NET Advantage: Features, Benefits, and Use CasesThe C# .NET Advantage: Features, Benefits, and Use CasesMay 05, 2025 am 12:01 AM

The advantages of C#.NET include: 1) Language features, such as asynchronous programming simplifies development; 2) Performance and reliability, improving efficiency through JIT compilation and garbage collection mechanisms; 3) Cross-platform support, .NETCore expands application scenarios; 4) A wide range of practical applications, with outstanding performance from the Web to desktop and game development.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.