web server - #include #include...

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

View Full Document Right Arrow Icon
#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);
Image of page 1

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

View Full Document Right Arrow Icon
} 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";
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern