Simple Web Server
web服務器hello world!-----簡單的socket通信實現。
HTTP
HTTP是Web瀏覽器與Web服務器之間通信的標準協議,HTTP指明了客戶端如何與服務器建立連接,如果從服務器請求數據,服務器如何響應請求,關閉連接。HTTP是使用TCP/IP協議進行傳輸數據的,也就是傳輸層利用TCP進行連接,進行可靠連接的。
詳情:點擊
請求
一般格式,如:
GET /index.html HTTP/1.0
Accept:text/html,text/plain
User-Agent: Lyn
Host:www.server.com
我們主要需要的信息在第一行,共包括三個字段。
- 第一個字段(GET)為請求動作類型:
- GET代表請求的操作,表示要求服務器返回資源的表示;
- HEAD表示只需要文件的首部;
- PUT表示向服務器上傳資源;
- POST主要是向服務器發送表單數據.
- 第二個字段(/index.html),標識服務器上所請求的資源的相對URL,必須要以"/"開頭,Web瀏覽器在發送請求的時候會自動加上服務器的主機名。
- 第三個字段(HTTP/1.0),客戶端理解的協議版本
注意:
每一個HTTP請求都要以兩個回車換行結束(\r\n\r\n)
GET發送查詢字符串主要直接將查詢字符串附加到URL后面,如下表示:GET /index.html/user=XXX&Age HTTP/1.0
響應
?一般格式,如
HTTP/1.1 200 OK
Date:Mon 15
Server:xxxxxx
Content-Type:text/html;
Content-length:xxx 代表文檔的多少個字節,不包含首部字節數
<html><head><title>Hello</title></head><body>Test</body></html>
我們需要大概構造這樣的一個格式來回復瀏覽器。
所以,必須包含第一行的狀態行、內容的格式、內容的長度和具體的內容。
實例
/** A Simple Web Server*/#include <stdio.h> #include <string.h> #include <stdlib.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <error.h>#define PORT 9000#define EOL "\r\n" #define EOL_SIZE 2int recv_new(int fd, char *buffer); void send_new(int fd, char *msg);int main(int argc, char* argv[]) {int serv_fd;int client_fd;int ret;pid_t pid;struct sockaddr_in serv_addr;struct sockaddr_in client_addr;int len = sizeof(struct sockaddr_in);serv_fd = socket(AF_INET, SOCK_STREAM, 0);if(serv_fd < 0){perror("create socket fail !\n");exit(1);}bzero(&serv_addr, sizeof(serv_addr));serv_addr.sin_family = AF_INET;serv_addr.sin_addr.s_addr = INADDR_ANY;serv_addr.sin_port = htons(PORT);int on = 1;ret = setsockopt(serv_fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(int));if(ret < 0){perror("setsockopt fail !\n");exit(1);}ret = bind(serv_fd, (struct sockaddr*)&serv_addr, sizeof(serv_addr));if(ret < 0){perror("bind fail !\n");exit(1);}ret = listen(serv_fd, 5);if(ret < 0){perror("listen fail !\n");exit(1);}while(1){client_fd = accept(serv_fd, (struct sockaddr*)&client_addr, &len);if(client_fd < 0){perror("accept fail !\n");continue;}char buffer[200];recv_new(client_fd, buffer);printf("recv buffer: %s\n", buffer);char content[] = "<head><head><title>index.html</title></head><body>hello world!</body>";char response[512];sprintf(response, "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nContent-Length: %d\r\n\r\n%s", strlen(content), content);send(client_fd, response, sizeof(response), 0);close(client_fd);}return 0; }int recv_new(int fd, char *buffer) {char *p = buffer; // Use of a pointer to the buffer rather than dealing with the buffer directlyint eol_matched = 0; // Use to check whether the recieved byte is matched with the buffer byte or notwhile (recv(fd, p, 1, 0) != 0) // Start receiving 1 byte at a time {if (*p == EOL[eol_matched]) // if the byte matches with the first eol byte that is '\r' {++eol_matched;if (eol_matched == EOL_SIZE) // if both the bytes matches with the EOL {*(p + 1 - EOL_SIZE) = '\0'; // End the stringreturn (strlen(buffer)); // Return the bytes recieved }} else {eol_matched = 0;}p++; // Increment the pointer to receive next byte }return (0); }本文轉自cococo點點博客園博客,原文鏈接:http://www.cnblogs.com/coder2012/p/3402221.html,如需轉載請自行聯系原作者
總結
以上是生活随笔為你收集整理的Simple Web Server的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: curl 伪装来路(referer)
- 下一篇: asp教程七:包含文件