web server

web server -...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: #include <winsock.h> #include <sys/stat.h> #include <iostream> using namespace std; #define SERVER_PORT 10000 //×Ô¶¨ÒåµÄ·þÎñ¶Ë¿Ú #define HOSTLEN 256 //Ö÷»úÃû³¤¶È #define BACKLOG 10 //ͬʱµÈ´ýµÄÁ¬½Ó¸öÊý int sendall(int s, char *buf, int *len) { int total = 0; // ÒѾ·¢ËÍ×Ö½ÚÊý int bytesleft = *len; //»¹Ê£Óà¶àÉÙ×Ö½Ú int n; while(total < *len) { n = send(s, buf+total, bytesleft, 0); if (n == -1) { break; } total += n; bytesleft -= n; } *len = total; // ·µ»Øʵ¼Ê·¢ËͳöÈ¥µÄ×Ö½ÚÊý return n==-1?-1:0; // ³É¹¦·¢ËÍ·µ»Ø0 ʧ°Ü-1 } void wrong_req(int sock) { char* error_head = "HTTP/1.0 501 Not Implemented\r\n"; //Êä³ö501´íÎó int len = strlen(error_head); if (sendall(sock, error_head, &len) == -1) { //Ïò¿Í»§·¢ËÍ printf("Sending failed!"); return; } char* error_type = "Content-type: text/plain\r\n"; len = strlen(error_type); if (sendall(sock, error_type, &len) == -1) { printf("Sending failed!"); return; } char* error_end = "\r\n"; len = strlen(error_end); if (sendall(sock, error_end, &len) == -1) { printf("Sending failed!"); return; } char* prompt_info = "The command is not yet completed\r\n"; len = strlen(prompt_info); if (sendall(sock, prompt_info, &len) == -1) { printf("Sending failed!"); return; } } bool not_exit(char* arguments) { struct stat dir_info; return (stat(arguments, &dir_info) == -1); } void file_not_found(char* arguments, int sock) { char* error_head = "HTTP/1.0 404 Not Found\r\n"; //¹¹Ôì404´íÎóhead int len = strlen(error_head); if (sendall(sock, error_head, &len) == -1) { //Ïò¿Í»§¶Ë·¢ËÍ printf("Sending error!"); return; } char* error_type = "Content-type: text/plain\r\n";...
View Full Document

This note was uploaded on 10/02/2011 for the course CSC 7103 taught by Professor Kannan,r during the Spring '08 term at LSU.

Page1 / 5

web server -...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online