http server 项目
xinetd:负责http底层的传输
![]()
![]()
![]()
![]()
![]()
![]()
第一步:
在/etc/xinetd.d下创建 micro_httpd文件
sudo vim micro_httpd
service micro_httpd{socket_type = streamprotocol = tcpwait = nouser = liuweiserver = /home/liuwei/http_protect/micro_httpdserver_args = /home/liuwei/wwwdisable = noflags = IPv4}
第二步:sudo vim /etc/services
在文件的最下面添加两句话
micro_httpd 8080/tcp
micro_httpd 8080/udp #Micro HTTP SERVER
第三步 : 重启xinetd
sudo /etc/init.d/xinetd restart
参考资料:
1.sudo aptitude install xinetd命令安装xinetd工具
2.sudo vi /etc/xinetd.d/micro_httpd创建micro_httpd文件并打开写入如下内容:
service micro_httpd
{
socket_type = stream
protocol = tcp
wait = no
user = whoami
server = /home/whoami/http-project/micro_httpd
server_args = /home/whoami/mh_html
disable = no
flags = IPv4
}
【特别注意】:文件名是micro_httpd而不是micro_httpd.conf
‘=’左边是tab,右边是一个空格
3.sudo vi /etc/services打开该文件,加入如下两行:
micro_httpd 2222/tcp #Micro HTTP server
micro_httpd 2222/udp #Micro HTTp server
4.sudo service xinetd restart重启xinetd
(以前是使用sudo /etc/init.d/xinetd restart, 是否互通待验证)
6.使用http://localhost:2222访问/home/whoami/mh_html下面的内容
项目实战:
改变工作目录 chdir
chdir("home/liuwei/demo");
然后可以fopen 创建
获取当前时间

获取文件属性
linux命令:stat 123.c
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <time.h>#include <sys/types.h>#include <dirent.h>#include <sys/stat.h>#include <unistd.h>void sys_err( char * str){printf( "%s\n" , str);exit(-1);}void show_dir(char * pathname , char * p){struct dirent * ptr;struct stat buf;DIR * dir = opendir(pathname);if( dir == NULL)sys_err("open dir error!");printf("<style>a{color:black;}.dir{color:red;font-size:25px;text-decoration:none}span{color:purple}td{width:25%%;text-align:left;}tr{height:35px}</style><table>");while( (ptr = readdir(dir)) != NULL ){if(ptr->d_name[0] == '.')continue;char tmp[1024] , css[10] = "dir";strcpy(tmp , pathname);strcat(tmp , "/");strcat(tmp , ptr->d_name);stat(tmp, &buf);if(strcmp( p , "/") == 0)p[0] = '\0';if(S_ISDIR(buf.st_mode) == 0)css[0] = '\0';printf("<tr><td><a class='%s' href='%s/%s'>%s</a></td><td>%s</td><td>%d</td></tr>",css,p,ptr->d_name,ptr->d_name,ctime(&buf.st_atime),(int)buf.st_size );}printf("</table><br/><hr/><span>apache_httpd</span>");fflush(stdout);exit(0);}void show_file( char * buf){FILE *fp;char * p = NULL , ch;char pathname[50] = "/home/liuwei/www";if((p = strtok( buf , " ")) && (p = strtok(NULL , " "))){struct stat buf;if( strcmp( p , "/") == 0)show_dir(pathname , p);strcat(pathname , p);stat(pathname, &buf);if(S_ISDIR(buf.st_mode))show_dir(pathname , p);fp = fopen( pathname , "r");if(fp == NULL)printf("404 not found\n");else{//printf("HTTP/1.1 200 OK\r\n");//printf("Content-Language:zh-ch\r\n");//printf("Content-Type:text/html;charset=utf-8\r\n\r\n");while(fread(&ch, sizeof(ch),1,fp) != 0 )fwrite(&ch,sizeof(ch),1,stdout);}}fflush(stdout);fclose( fp );}void write_log( void ){char buf[8192];time_t t ;int i = 1;FILE * fp = fopen("log.txt","a");if( fp == NULL )sys_err("fopen log.txt error");time(&t);fputs( ctime(&t) , fp);fputs( "-----------------------\n" , fp);while(( fgets(buf,sizeof(buf),stdin) != NULL ) && strlen(buf) != 2){fputs( buf , fp );if(i++ == 1)show_file( buf );}fprintf( fp , "\n");fclose( fp );}int main( int argc , char * argv[]){if(chdir("/home/liuwei/http-project/"))sys_err("change dir error");write_log();return 0;}
Makefile:
all:clean httpd@echo "123456"sudo /etc/init.d/xinetd restarthttpd:gcc liuwei_httpd.c -o liuwei_httpdclean:rm -rf liuwei_httpd log.txt




浙公网安备 33010602011771号