Web server implementation

Source: Internet
Author: User
Tags strtok

C language is used to implement a simple web server. The program supports requests in html, css, js, jpg, and gif formats [cpp] # include <stdio. h> # include <fcntl. h> # include <netinet/in. h> # include <arpa/inet. h> # include <unistd. h> # include <sys/types. h> # include <sys/stat. h> # include <string. h> # include <stdlib. h> # define PORT 12345 # define WebRoot/home/meng/webpage/*** whether the string s1 ends with s2 */int endsWith (char s1 [], char s2 []) {int len1 = strlen (s1); int len2 = strlen (s2); int I = len1-1, j = len2-1; if (l En1 <len2) return 0; for (; I >=0 & j> = 0; I --, j --) {if (s1 [I]! = S2 [j]) return 0;} return 1;} int main () {int sockfd, fd, client; struct sockaddr_in addr; int len = sizeof (addr ); char recbuf [2048]; // socket connection if (sockfd = socket (AF_INET, SOCK_STREAM, 0) <0) {perror ("socket. \ n "); exit (1) ;}// set IP port bzero (& addr, sizeof (addr); addr. sin_family = AF_INET; addr. sin_port = htons (PORT); addr. sin_addr.s_addr = inet_addr ("127.0.0.1"); // bind if (bind (sockfd, (struct sockaddr *) & addr, si Zeof (addr) <0) {perror ("bind. \ n "); exit (1) ;}// maximum number of listen connections if (listen (sockfd, 10) <0) {perror (" listen. \ n "); exit (1);} printf (" listening... \ n "); while (1) {// obtain the client ID if (client = accept (sockfd, (struct sockaddr *) & addr, & len) <0) {perror ("accept. \ n "); exit (1) ;}// read Request Header int buf = 0; if (buf = read (client, recbuf, 2048)> = 0) {recbuf [buf] = '\ 0';} // printf ("% s \ n", recbuf); char * token = strtok (recbuf ,""); // obtain HT The middle part of the first line of the TP request, that is, URI if (token! = NULL) {token = strtok (NULL, "");} char type [256];/*** varies depending on the extension at the end of the requested resource, * different mine response types */if (endsWith (token ,". jpg ") {strcpy (type," image/jpeg \ r \ nAccept-Ranges: bytes ");} else if (endsWith (token ,". gif ") {strcpy (type," image/gif ");} else if (endsWith (token ,". js ") {strcpy (type," text/javascript ");} else if (endsWith (token ,". css ") {strcpy (type," text/css ");} else {strcpy (type," text/html ");} // read the file content char fi LePath [1024]; char content [2048*512]; int fileSize; strcpy (filePath, "/home/meng/webpage"); strcat (filePath, token ); fd = open (filePath, O_RDONLY, 0766); if (fd =-1) {strcpy (content, "Not Found");} else {fileSize = read (fd, content, sizeof (content); // printf ("% d \ n", strlen (content); content [fileSize] = '\ 0 '; if (fileSize <= 0) strcpy (content, "Not Found"); close (fd);} if (fileSize <= 0) {fileSize = strlen ("Not Found ");} Char sendBuf [2048*1024]; char tmp [20]; sprintf (tmp, "% d", fileSize); memset (sendBuf, '\ 0 ', sizeof (sendBuf); // returns the header information of the HTTP Response // all returned states are 200. Currently, other response states such as 404 are not judged. strcat (sendBuf, "HTTP/1.1 200 OK \ r \ n"); strcat (sendBuf, "Server: MENGXIANLU \ r \ n"); strcat (sendBuf, "Content-Length :"); strcat (sendBuf, tmp); strcat (sendBuf, "\ r \ n"); strcat (sendBuf, "Content-Type:"); strcat (sendBuf, type ); strcat (sendBuf, "\ r \ n ");// The following is extremely important, indicating the end Of the response header. // After the header ends, strcat (sendBuf, "\ r \ n") is included in the body;/** response header END */int I, j; /** response content (body) * // ** you cannot use strcat to connect strings. * // ** If an image is requested, its bytecode may contain '\ 0'. If strcat is used, information truncation may occur */for (I = 0, j = strlen (sendBuf); I <fileSize; I ++, j ++) {sendBuf [j] = content [I];} /** everything is ready */sendBuf [j] = '\ 0';/** the result is returned to the client */write (client, sendBuf, j ); close (client) ;}close (sockfd); return 0 ;}the following is a simple test performed from the browser after running the program.

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.