It's not hard to implement a simple Web server under Linux. One of the simplest HTTP servers is simply an advanced file server that receives HTTP requests from the client (browser), parses the request, processes the request, and then sends back the data like the client. In most cases, (GET, Post command), the service is sent back to the client are files (HTML documents, pictures, JavaScript scripts, etc.).
The following is a very simple demo of HTTP server, although it handles only get requests and sends a single file, but basically shows the framework of the Web server. My example attempts to streamline functionality and structure so that the basic structure of an HTTP server is at a glance.
[CPP]View PlainCopy
- #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
- A well-defined HTML page, in fact the Web server basically reads the HTML file from the local file system
- Const Static char http_error_hdr[] = "http/1.1 404 Not Found\r\ncontent-type:text/html\r\n\r\n";
- Const Static char http_html_hdr[] = "http/1.1 ok\r\ncontent-type:text/html\r\n\r\n";
- Const Static char http_index_html[] =
- "
- "<body>
- "<p>this is a just small test page.</body>
- After parsing the file to the HTTP request, send the file in the local file system
- Here, we handle the request to the index file, send our pre-ordered HTML file
- Oh, Basics!
- int Http_send_file (char *filename, int sockfd)
- {
- if (!strcmp (filename, "/")) {
- //Send HTTP response message via write function; message includes HTTP response header and response content--html file
- Write (SOCKFD, HTTP_HTML_HDR, strlen (HTTP_HTML_HDR));
- Write (SOCKFD, http_index_html, strlen (http_index_html));
- }
- else{
- //File not found in case of sending 404error response
- printf ("%s:file not find!\n", filename);
- Write (SOCKFD, HTTP_ERROR_HDR, strlen (HTTP_ERROR_HDR));
- }
- return 0;
- }
- HTTP request parsing
- void serve (int sockfd) {
- Char Buf[buf_len];
- Read (SOCKFD, buf, Buf_len);
- if (!strncmp (buf, "GET", 3)) {
- char *file = buf + 4;
- char *space = STRCHR (file, ');
- *space = ' + ';
- Http_send_file (file, SOCKFD);
- }
- else{
- //Other HTTP request processing, such as Post,head. Here we only deal with get
- printf ("Unsupported request!\n");
- return;
- }
- }
- void Main () {
- int sockfd,err,newfd;
- struct sockaddr_in addr;
- //Establish TCP sockets
- SOCKFD = socket (af_inet, sock_stream, 0);
- if (SOCKFD < 0) {
- Perror ("Socket Creation failed!\n");
- return;
- }
- memset (&addr, 0, sizeof (addr));
- addr.sin_family = af_inet;
- //Note that the port number must first be converted to a network byte order using htons, otherwise the actual port of the binding
- //May be different from what you need
- Addr.sin_port = htons (Server_port);
- ADDR.SIN_ADDR.S_ADDR = Inaddr_any;
- if (Bind (SOCKFD, (struct sockaddr *) &addr, sizeof (struct sockaddr_in))) {
- Perror ("socket binding failed!\n");
- return;
- }
- Listen (SOCKFD, 128);
- for (;;) {
- //uninterrupted receive HTTP requests and processing, here using single-threaded, in real-world consideration of the efficiency of general multithreading
- NEWFD = Accept (SOCKFD, NULL, NULL);
- Serve (NEWFD);
- Close (NEWFD);
- }
- }
Linux under HTTP Server