Windows下C++实现WEB服务器

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

/topic/368943

自己研究了好几天终于写出来一个,哈哈,当然也从网上得到了很多的帮助拉。谢谢大家咯!这个版本还不是很完善,但Web服务器的基本框架已经出来了,还有部分的功能需要进行进一步的测试和修改。虽然说C的开发比较慢,对于程序员来说比较难以操作,但通过用C写这些很底层的东西,可以更好的了解的象java的socket中的工作原理。有一定的帮助!

以下是源代码:

#include

#include

#include

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";

len = strlen(error_type);

if (sendall(sock, error_type, &len) == -1) {

printf("Sending error!");

return;

}

char* error_end = "\r\n";

len = strlen(error_end);

if (sendall(sock, error_end, &len) == -1) {

printf("Sending error!");

return;

}

char prompt_info[50] = "Not found: ";

strcat(prompt_info, arguments);

len = strlen(prompt_info);

if (sendall(sock, prompt_info, &len) == -1) { //输出未找到的文件

printf("Sending error!");

return;

}

}

void send_header(int send_to, char* content_type) {

char* head = "HTTP/1.0 200 OK\r\n"; //正确的头部信息

int len = strlen(head);

if (sendall(send_to, head, &len) == -1) { //向连接的客户端发送数据 printf("Sending error");

return;

}

if (content_type) { //content_type不为空

char temp_1[30] = "Content-type: "; //准备好要连接的字串

strcat(temp_1, content_type); //构造content_type

strcat(temp_1, "\r\n");

len = strlen(temp_1);

if (sendall(send_to, temp_1, &len) == -1) {

printf("Sending error!");

return;

}

}

}

char* file_type(char* arg) {

char * temp; //临时字符串指针

if ((temp=strrchr(arg,'.')) != NULL) { //取得后缀

return temp+1;

}

return ""; //如果请求的文件名中没有. 则返回空串

}

void send_file(char* arguments, int sock) {

char* extension = file_type(arguments); //获得文件后缀名

char* content_type = "text/plain"; //初始化type='text/plain'

FILE* read_from; //本地文件指针从该文件中读取.html .jpg等

int readed = -1; //每次读得的字节数

if (strcmp(extension, "html") == 0) { //发送内容为html

content_type = "text/html";

}

相关文档
最新文档