It is not difficult to implement a simple web server in Linux. A simple HTTP server is just an advanced file server that constantly receives HTTP requests sent by clients (browsers), parses requests, Processes requests, and then sends data back like a client. In most cases, (get and POST commands), all files (HTML documents, images, JavaScript scripts, etc.) that the service requests are sent back to the client ).
The following is a simple HTTP server demo. Although it only processes get requests and sends a single file, it basically shows the Web Server framework. My example tries to streamline the functions and structure so that the basic structure of an HTTP server is clear at a glance.
# Include <sys/socket. h> # include <errno. h> # include <netinet/in. h> # include <string. h> # include <stdio. h> # define buf_len 1028 # define server_port 8080 // defined HTML page, in actual cases, the web server basically reads the HTML file const static char http_error_hdr [] = "HTTP/1.1 404 Not found \ r \ ncontent-type: text/html \ r \ n "; const static char http_html_hdr [] =" HTTP/1.1 200 OK \ r \ ncontent-type: text/html \ r \ n "; const static char http_index _ HTML [] = "<HTML>
less than 80 codes can be used to implement an ultra-simple HTTP server.