配置环境
1,安装 xinetd :类似消息队列
# sudo apt-get install xinetd 或者 sudo aptitude show xinetd
2,打开 /etc/xinetd.d 文件下面创建 xhttp.d(可以随意取名)(注意三个文件名要统一)
mkdir xhttp.d //创建文件
(1,向文件中写入 配置
services xhttpd
{
socket_type = stream
protocol = tcp
wait = no
user = nobody
server = /home/chenli/dir/xhttpd //服务的执行2文件
server_args = /home/chenli/dir
disable = no
flags = IPv4
}
3, 打开/etc/services文件
到底 添加服务器的端口号
xhttpd 201601/tcp #xhttpd server
xhttpd 201601/tup #xhttpd server
4,重启xinetd 服务器 sudo service xinetd restart
程序的框架
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 4096
void send_err(错误号, 错误名称, 错误描述)
{
<html>
<head><title>错误号 错误名称</title></head>
<body>
错误描述
</body>
</html>
}
void send_handers(char *type)
{
HTTP/1.1 200 OK
Content-Type: text/plain;charset=iso-8859-1
Connection:close
\r\n
}
int main(int argc, char *argc[])
{
char line[N];
char method[N], path[N], protocol[N];
char *file; //文件名
struct stat sbuf; //
FILE *fp;
char *ich; //子
if ( argc != 2)
//send_err();
if ( chdir(argv[1]) == -1) //判断文件路径是否是正确 工作目录
//send_err();
if (fgets(line, N, stdin) == NULL) //GET /hello.c HTTP/1.1
//send_err();
if(sscanf(line, "%[^ ] %[^ ] %[^ ]", method, path, protocol) != 3)
//send_err();
while ( fgets( line, N, stdin) != NULL) //判断文件有没有末
if ( strcmp(line, "\r\n"); //浏览器的发过来的信息
break;
if ( strcmp(method, "GET") != 0)
//send_err();
if (path[0] !='/') // 这个 反斜杠代表是 /home/chenli/dir
//send_err();
file = path + 1; //获取文件名
if (stat(file, &sbuf) < 0) //没有文件存在
//send_err();
fp = fopen(file, "r");
if ( fp == NULL) //判断有没有读取的权限
//send_err();
//send_hander();
while((ich = getc(fp)) != EOF)
putchar(ich);
fflush(stdout);
fclose(fp); //关闭文件流
return 0;
}