Implementation of a simple Web Server

Source: Internet
Author: User

Yesterday, the source code of the ani-server was added to Source Forge. There are only a few hundred lines of code, and there is already a basic web server function.

I realized it myself by referring to the idea.

Basic Ideas:

1) Get a socket FD --> bind --> listen --> select --> Accept,

Obtain a cli_fd that can communicate with the browser requesting the service.

2) read data from cli_fd, determine whether it is an http get, obtain the requested file, and write the file data to cli_fd.

Source code:

/*  * author: dengzhaoqun * date: 2011/07/21 * platform: linux */#include <sys/select.h>#include <stdio.h>#include <sys/types.h>#include <sys/socket.h>#include <stdlib.h>#include <string.h>#include <arpa/inet.h>#include <netinet/in.h>#include <fcntl.h>#include <sys/stat.h>#include <unistd.h>#define DEFAULT_PAGE "index.html"#define WEB_ROOT "/home/xiaodeng/aniroot"void connection(int fd);int recv_new(int fd, char *str);void send_new(int fd, char *str);int get_file_size(int fd);int main(){int ser_fd, cli_fd, max_fd, fd;struct sockaddr_in ser_addr;struct sockaddr_in cli_addr;int ser_addr_len, cli_addr_len;fd_set readfds, master;int ret;ser_fd = socket(AF_INET, SOCK_STREAM, 0);if(ser_fd == -1){perror("socket failed");exit(1);}ser_addr_len = sizeof(ser_addr);memset(&ser_addr, 0, ser_addr_len);ser_addr.sin_family = AF_INET;ser_addr.sin_addr.s_addr = inet_addr("127.0.0.1");ser_addr.sin_port = htons(80);ret = bind(ser_fd, (struct sockaddr *)&ser_addr, ser_addr_len);if(ret == -1){perror("bind failed");exit(1);}listen(ser_fd, 5);FD_ZERO(&master);FD_SET(ser_fd, &master);max_fd = ser_fd;while(1){readfds = master;ret = select(max_fd + 1, &readfds, 0, 0, 0);if(ret == -1){perror("select failed");}else{for(fd = 0; fd < max_fd + 1; fd ++){if(FD_ISSET(fd, &readfds)){if(fd == ser_fd){cli_addr_len = sizeof(cli_addr);memset(&cli_addr, 0, cli_addr_len);cli_fd = accept(ser_fd,       (struct sockaddr *)&cli_addr,(socklen_t *)&cli_addr_len);if(cli_fd > max_fd){max_fd = cli_fd;}FD_SET(cli_fd, &master);}else{connection(fd);FD_CLR(fd, &master);}}}}}}/* main() */voidconnection(int fd){char request[1000], resource[1000], *ptr;int ret;int page_fd;ret = recv_new(fd, request);ptr = strstr(request, " HTTP/");if(ptr == NULL){perror("Not HTTP request");}else{*ptr = 0;ptr = NULL;if(strncmp(request, "GET ", 4) == 0){ptr = request + 4;}if(strncmp(request, "HEAD ", 5) == 0){ptr = request + 5;}if(ptr == NULL){perror("Unknown Request. \n");}else{memset(resource, 0, sizeof(resource));strncpy(resource, WEB_ROOT, sizeof(resource));strncat(resource, ptr, sizeof(resource) - sizeof(WEB_ROOT) -1);if(ptr[strlen(ptr) -1] == '/' ){strncat(resource, DEFAULT_PAGE, sizeof(resource)- strlen(resource)- sizeof(DEFAULT_PAGE)- 1);}if((page_fd = open(resource, O_RDONLY)) == -1){perror("open failed, file not found");send_new(fd, "HTTP/1.1 404 NOT FOUND\r\n");send_new(fd, "

Compile and run the program. Enter "http: // localhost/filename.html" in the browser to display the corresponding HTML file in the web_root directory.

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.