Linux under HTTP Server

Source: Internet
Author: User
Tags htons

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
  1. #include <sys/socket.h>
  2. #include <errno.h>
  3. #include <netinet/in.h>
  4. #include <string.h>
  5. #include <stdio.h>
  6. #define Buf_len 1028
  7. #define SERVER_PORT 8080
  8. A well-defined HTML page, in fact the Web server basically reads the HTML file from the local file system
  9. Const Static char http_error_hdr[] = "http/1.1 404 Not Found\r\ncontent-type:text/html\r\n\r\n";
  10. Const Static char http_html_hdr[] = "http/1.1 ok\r\ncontent-type:text/html\r\n\r\n";
  11. Const Static char http_index_html[] =
  12. "
  13. "<body>
  14. "<p>this is a just small test page.</body>
  15. After parsing the file to the HTTP request, send the file in the local file system
  16. Here, we handle the request to the index file, send our pre-ordered HTML file
  17. Oh, Basics!
  18. int Http_send_file (char *filename, int sockfd)
  19. {
  20. if (!strcmp (filename, "/")) {
  21. //Send HTTP response message via write function; message includes HTTP response header and response content--html file
  22. Write (SOCKFD, HTTP_HTML_HDR, strlen (HTTP_HTML_HDR));
  23. Write (SOCKFD, http_index_html, strlen (http_index_html));
  24. }
  25. else{
  26. //File not found in case of sending 404error response
  27. printf ("%s:file not find!\n", filename);
  28. Write (SOCKFD, HTTP_ERROR_HDR, strlen (HTTP_ERROR_HDR));
  29. }
  30. return 0;
  31. }
  32. HTTP request parsing
  33. void serve (int sockfd) {
  34. Char Buf[buf_len];
  35. Read (SOCKFD, buf, Buf_len);
  36. if (!strncmp (buf, "GET", 3)) {
  37. char *file = buf + 4;
  38. char *space = STRCHR (file, ');
  39. *space = ' + ';
  40. Http_send_file (file, SOCKFD);
  41. }
  42. else{
  43. //Other HTTP request processing, such as Post,head. Here we only deal with get
  44. printf ("Unsupported request!\n");
  45. return;
  46. }
  47. }
  48. void Main () {
  49. int sockfd,err,newfd;
  50. struct sockaddr_in addr;
  51. //Establish TCP sockets
  52. SOCKFD = socket (af_inet, sock_stream, 0);
  53. if (SOCKFD < 0) {
  54. Perror ("Socket Creation failed!\n");
  55. return;
  56. }
  57. memset (&addr, 0, sizeof (addr));
  58. addr.sin_family = af_inet;
  59. //Note that the port number must first be converted to a network byte order using htons, otherwise the actual port of the binding
  60. //May be different from what you need
  61. Addr.sin_port = htons (Server_port);
  62. ADDR.SIN_ADDR.S_ADDR = Inaddr_any;
  63. if (Bind (SOCKFD, (struct sockaddr *) &addr, sizeof (struct sockaddr_in))) {
  64. Perror ("socket binding failed!\n");
  65. return;
  66. }
  67. Listen (SOCKFD, 128);
  68. for (;;) {  
  69. //uninterrupted receive HTTP requests and processing, here using single-threaded, in real-world consideration of the efficiency of general multithreading
  70. NEWFD = Accept (SOCKFD, NULL, NULL);
  71. Serve (NEWFD);
  72. Close (NEWFD);
  73. }
  74. }

Linux under HTTP Server

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.