免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1538 | 回复: 3
打印 上一主题 下一主题

[C] 一个简单的WWW服务器程序,它怎么了? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-04-17 15:15 |只看该作者 |倒序浏览
问题: 为什么我使用守护进程,列表目录不是当前目录?
注销initDaemon函数,一切都正常了,请教大家是怎么回事? 万分感谢

  1. #include <unistd.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <unistd.h>
  6. #include <sys/socket.h>
  7. #include <netinet/in.h>
  8. #include <sys/types.h>
  9. #include <netdb.h>
  10. #include <sys/utsname.h>
  11. #include <sys/types.h>
  12. #include <sys/stat.h>
  13. #include <sys/wait.h>

  14. #define MAXCLIENT 50
  15. #define SERVERPORT 80
  16. #define DEFAULTHTML index.html


  17. extern int pthreadAction(char * request, int cfd);
  18. extern int isDir(char *filepath);
  19. extern void header();
  20. extern int openFile(char *path,int cfd);

  21. int main(int argc,char *agrv[]) {
  22. int serverfd,clientfd;
  23. FILE * cfp;
  24. char request[BUFSIZ];
  25. pid_t pid;

  26. //initDaemon();
  27. serverfd = initSocket(SERVERPORT);

  28. while (1) {
  29.     clientfd = accept(serverfd,NULL,NULL);
  30.     if ((pid =fork()) < 0) {
  31.         die("fork");
  32.     }
  33.     if (pid == 0){
  34.         close(serverfd);
  35.         cfp = fdopen(clientfd,"r");
  36.         fgets(request,BUFSIZ,cfp);
  37.         pthreadAction(request,clientfd);
  38.         fclose(cfp);
  39.         close(clientfd);
  40.         _exit(0);
  41.     }else {
  42.          if(waitpid(pid,NULL,0)!= pid){
  43.             die("wait");
  44.          }
  45.         close(clientfd);
  46.     }

  47. }
  48.     close(serverfd);
  49. }

  50. int pthreadAction(char * request, int cfd) {

  51. char cmd[BUFSIZ],filepath[BUFSIZ];
  52. strcpy(filepath,".");
  53. if((sscanf(request,"%s%s",cmd,(filepath+1))) != 2) {
  54.   // exit;
  55. }
  56. printf("request:%s",request);
  57. printf("cmd:%s\n",cmd);
  58. printf("filepath:%s\n",filepath);

  59. if (strcmp(cmd,"GET") != 0){
  60.     return;
  61. }
  62. if ((isDir(filepath)) == 0) {
  63.     if ((listDir(filepath,cfd)) == -1) {
  64.         die("listdir");
  65.     }
  66. }else{
  67.     if ((openFile(filepath,cfd)) != 0){
  68.         die("openFile");
  69.     }
  70. }
  71. }

  72. int openFile(char * path,int cfd) {
  73.     FILE * cfp;
  74.     cfp = fdopen(cfd,"w");
  75.     header(cfp,"text/html;charset=utf-8");
  76.     fprintf(cfp,"<html><title>我爱你,WEBSERVER。</title><body><br><p>我是谁??</p></body></html>");
  77.     fprintf(cfp,"<a href=\"\">test</a>");
  78.     fclose(cfp);
  79.     return 0;
  80. }

  81. void choiceType(char * filepath) {

  82. }

  83. int isDir(char *filepath) {
  84.     struct stat info;
  85.     if (stat(filepath,&info)!=-1 && S_ISDIR(info.st_mode)) {
  86.         return 0;
  87.     }else{
  88.         return -1;
  89.     }
  90. }

  91. int listDir(char *path,int cfd) {
  92.     FILE * cfp;
  93.     cfp=fdopen(cfd,"w");
  94.     header(cfp,"text/plain;charset=utf-8");
  95.     fflush(cfp);
  96.     dup2(cfd,1);
  97.     dup2(cfd,2);
  98.     if (execlp("ls","ls","-l",NULL,NULL) == -1) {
  99.         die("execlp");
  100.         return -1;
  101.     }
  102.     fclose(cfp);
  103.     return 0;
  104. }

  105. void header(FILE *cfp,char * type) {
  106.     fprintf(cfp,"HTTP/1.0 200 OK\r\n");
  107.     fprintf(cfp,"Content-type: %s\r\n",type);
  108.     fprintf(cfp,"\r\n");
  109. }

  110. void error404() {

  111. }

  112. // install daemon service
  113. int initDaemon(void) {
  114. pid_t pid;
  115. int i;
  116. if ((pid = fork()) < 0) {
  117.         die("System fork");
  118. }
  119. if (pid != 0) {
  120.         _exit(0);
  121. }
  122. if ((setsid()) == -1){
  123.         die("setsid");
  124. }
  125. if ((pid = fork()) < 0) {
  126.         die("System fork");
  127. }
  128. if (pid != 0) {
  129.         _exit(0);
  130. }
  131. if ((chdir("/tmp")) == -1) {
  132.         die("chdir");
  133. }
  134. for (i =0; i < 3; i++ ){
  135.         close(i);
  136. }
  137. if ((umask(0)) == -1){
  138.         die("umask");
  139. }
  140. return 0;
  141. }
  142. // End initdaemon

  143. // init Socket service
  144. int initSocket(int ServerPort) {
  145. int serverfd;
  146. struct sockaddr_in serveraddr;
  147. int sockopt = 1;

  148. memset(&serveraddr,0,sizeof(serveraddr));
  149. serveraddr.sin_family = AF_INET;
  150. serveraddr.sin_port = htons(ServerPort);
  151. serveraddr.sin_addr.s_addr = INADDR_ANY;

  152. if ((serverfd = socket(AF_INET,SOCK_STREAM,0)) == -1) {
  153.         die("socket");
  154. }

  155. setsockopt(serverfd,SOL_SOCKET,SO_REUSEADDR, &sockopt, sizeof(sockopt));

  156. if ((bind(serverfd,(struct sockaddr * )&serveraddr,sizeof(serveraddr))) == -1) {
  157.         die("bind");
  158. }
  159. if ((listen(serverfd,MAXCLIENT)) == -1) {
  160.         die("listen");
  161. }
  162. return serverfd;
  163. }

  164. // system die & return
  165. int die(const char * msg) {
  166. perror(msg);
  167. exit(1);
  168. }
复制代码

论坛徽章:
5
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:53:172015亚冠之水原三星
日期:2015-06-02 16:34:202015年亚冠纪念徽章
日期:2015-10-19 18:13:37程序设计版块每日发帖之星
日期:2015-11-08 06:20:00
2 [报告]
发表于 2009-04-17 19:24 |只看该作者

  1. if ((chdir("/tmp")) == -1) {
  2.         die("chdir");
  3. }
复制代码

initDaemon()中不是有这句么

论坛徽章:
0
3 [报告]
发表于 2009-04-20 16:48 |只看该作者
太大意了。。 感谢。。

论坛徽章:
1
巨蟹座
日期:2013-12-30 17:06:34
4 [报告]
发表于 2009-04-20 17:58 |只看该作者
mark
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP