您的位置:首页 > 理论基础 > 计算机网络

Tinyhttp服务器编译运行

2016-05-29 21:02 555 查看

Tinyhttp服务器编译运行

源码下载

下载Tinyhttp源码,网址http://sourceforge.net/projects/tinyhttpd/files/latest/download

修改httpd.c源码

1、声明函数修改如下
//void accept_request(int);
void *accept_request(void *);
2、定义函数修改如下:
//void accept_request(int client)
void *accept_request(void *client1)
{
// 新增下面一行代码
int client = *(int *)client1;
...
if (strcasecmp(method, "GET") && strcasecmp(method, "POST"))
{
unimplemented(client);
//return;
// 新增下面一行代码
return NULL;
}
...
close(client);
// 新增返回值
return NULL;
}
3、startup函数中
//int namelen = sizeof(name);
socklen_t namelen = sizeof(name);
4、main函数中
//int client_name_len = sizeof(client_name);
socklen_t client_name_len = sizeof(client_name);
5、main函数中
//if (pthread_create(&newthread , NULL, accept_request, client_sock) != 0)
if (pthread_create(&newthread , NULL, accept_request, (void *)&client_sock) != 0)


修改make文件

#gcc -W -Wall -lsocket -lpthread -o httpd httpd.c
gcc -W -Wall -o httpd httpd.c -lpthread


进行编译

执行make
编译成功后生成httpd可执行文件


启动httd服务

由于使用的是随机端口,故打开浏览器,输入http://127.0.0.1:随机端口号即可 。


成功访问界面

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息