首页 > 代码库 > 8.23 http server 项目

8.23 http server 项目

xinetd:负责http底层的传输
第一步:
在/etc/xinetd.d下创建  micro_httpd文件
sudo vim micro_httpd
  1. service micro_httpd
  2. {
  3. socket_type = stream
  4. protocol = tcp
  5. wait = no
  6. user = liuwei
  7. server = /home/liuwei/http_protect/micro_httpd
  8. server_args = /home/liuwei/www
  9. disable = no
  10. flags = IPv4
  11. }
第二步: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






  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <time.h>
  5. #include <sys/types.h>
  6. #include <dirent.h>
  7. #include <sys/stat.h>
  8. #include <unistd.h>
  9. void sys_err( char * str)
  10. {
  11. printf( "%s\n" , str);
  12. exit(-1);
  13. }
  14. void show_dir(char * pathname , char * p)
  15. {
  16. struct dirent * ptr;
  17. struct stat buf;
  18. DIR * dir = opendir(pathname);
  19. if( dir == NULL)
  20. sys_err("open dir error!");
  21. 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>");
  22. while( (ptr = readdir(dir)) != NULL )
  23. {
  24. if(ptr->d_name[0] == ‘.‘)
  25. continue;
  26. char tmp[1024] , css[10] = "dir";
  27. strcpy(tmp , pathname);
  28. strcat(tmp , "/");
  29. strcat(tmp , ptr->d_name);
  30. stat(tmp, &buf);
  31. if(strcmp( p , "/") == 0)
  32. p[0] = ‘\0‘;
  33. if(S_ISDIR(buf.st_mode) == 0)
  34. css[0] = ‘\0‘;
  35. printf("<tr><td><a class=‘%s‘ href=http://www.mamicode.com/‘%s/%s‘>%s%s%d",css,p,ptr->d_name,ptr->d_name,ctime(&buf.st_atime),(int)buf.st_size );
  36. }
  37. printf("</table><br/><hr/><span>apache_httpd</span>");
  38. fflush(stdout);
  39. exit(0);
  40. }
  41. void show_file( char * buf)
  42. {
  43. FILE *fp;
  44. char * p = NULL , ch;
  45. char pathname[50] = "/home/liuwei/www";
  46. if((p = strtok( buf , " ")) && (p = strtok(NULL , " ")))
  47. {
  48. struct stat buf;
  49. if( strcmp( p , "/") == 0)
  50. show_dir(pathname , p);
  51. strcat(pathname , p);
  52. stat(pathname, &buf);
  53. if(S_ISDIR(buf.st_mode))
  54. show_dir(pathname , p);
  55. fp = fopen( pathname , "r");
  56. if(fp == NULL)
  57. printf("404 not found\n");
  58. else
  59. {
  60. //printf("HTTP/1.1 200 OK\r\n");
  61. //printf("Content-Language:zh-ch\r\n");
  62. //printf("Content-Type:text/html;charset=utf-8\r\n\r\n");
  63. while(fread(&ch, sizeof(ch),1,fp) != 0 )
  64. fwrite(&ch,sizeof(ch),1,stdout);
  65. }
  66. }
  67. fflush(stdout);
  68. fclose( fp );
  69. }
  70. void write_log( void )
  71. {
  72. char buf[8192];
  73. time_t t ;
  74. int i = 1;
  75. FILE * fp = fopen("log.txt","a");
  76. if( fp == NULL )
  77. sys_err("fopen log.txt error");
  78. time(&t);
  79. fputs( ctime(&t) , fp);
  80. fputs( "-----------------------\n" , fp);
  81. while(( fgets(buf,sizeof(buf),stdin) != NULL ) && strlen(buf) != 2)
  82. {
  83. fputs( buf , fp );
  84. if(i++ == 1)
  85. show_file( buf );
  86. }
  87. fprintf( fp , "\n");
  88. fclose( fp );
  89. }
  90. int main( int argc , char * argv[])
  91. {
  92. if(chdir("/home/liuwei/http-project/"))
  93. sys_err("change dir error");
  94. write_log();
  95. return 0;
  96. }

Makefile:
  1. all:clean httpd
  2. @echo "123456"
  3. sudo /etc/init.d/xinetd restart
  4. httpd:
  5. gcc liuwei_httpd.c -o liuwei_httpd
  6. clean:
  7. rm -rf liuwei_httpd log.txt




         






来自为知笔记(Wiz)


8.23 http server 项目