Advertisement
KOCSIE

Server

Oct 12th, 2020 (edited)
2,767
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.48 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <sys/socket.h>
  5. #include <sys/types.h>
  6. #include <arpa/inet.h>
  7. #include <netinet/in.h>
  8. #include <errno.h>
  9. #include <netdb.h>
  10. #include <unistd.h>
  11. #include <ctype.h>
  12.  
  13.  
  14. #define  MYPORT    12345
  15. #define  BACKLOG   5
  16.  
  17. int KEY_QUIT = 0;
  18. static char copybuf[16384]={0};
  19. #define DEBUG 1
  20.  
  21.  
  22.  
  23. int copy(FILE *read_f, FILE * write_f) //複製html文件
  24. {
  25.     int n;
  26.     int wrote;
  27.     n = fread(copybuf,1,sizeof(copybuf),read_f);
  28.     wrote = fwrite(copybuf,n,1,write_f);
  29.     return 0;
  30. }
  31.  
  32. int DoHTML(FILE *f, char * name)
  33. {
  34.     FILE *infile;
  35.     infile = fopen(name,"r"); //打開Client端請求的html文件
  36.  
  37.     printf("Sending mesg...\n");
  38.     copy(infile,f); //複製Client客戶端請求的html文件
  39.     printf("Complete sending.\n");
  40.     fgets(copybuf,1000,infile);
  41.     printf("\n");
  42.     puts(copybuf);
  43.     printf("\n");
  44.     fread(copybuf,1,sizeof(copybuf),f);
  45.     printf("DoHTML->fread done");
  46.     fclose(infile);
  47.     return 0;
  48. }
  49.  
  50. int ParseReq(FILE *f, char *r) //分析當中的內容,提取client請求的文件名
  51. {
  52.     char *bp;
  53.     char *c;
  54.     #ifdef DEBUG
  55.     #endif
  56.     while(*(++r) != ' '); //去掉請求內容開頭的空格部分
  57.     while(isspace(*r)) r++; //判斷輸入字符是否為空格/換行等,去掉所有這些字符
  58.     while(*r == '/') r++; //去掉所有的'/'字符
  59.     bp = r; //得到文件名的起始
  60.     while(*r && (*r != ' ') && (*r != '?')) r++;
  61.     *r = 0;
  62.     c = bp;
  63.     printf("The Request FileName is %s\n", c);
  64.     DoHTML(f, c); //得到文件名之後處理請求
  65.     return 0;
  66. }
  67.  
  68. int HandleConnect(int fd)
  69. {
  70.     FILE *f;
  71.     char buf[160];
  72.     f = fdopen(fd, "a+"); //打開對應文件名的html文件
  73.     setbuf(f, 0); //清除f文件緩存
  74.     fgets(buf, 150, f); //從Client請求中獲取前面150個字符
  75.     #ifdef DEBUG
  76.     #endif
  77.     ParseReq(f, buf); //分析請求的內容
  78.     printf("ParseReq done");
  79.     fflush(f);
  80.     printf("free success");
  81.     fclose(f);
  82.     return 1;
  83. }
  84.  
  85. void * key(void *data)
  86. {
  87.     int c;
  88.     for(;;)
  89.     {
  90.     c = getchar();
  91.         if(c == 'q'||c == 'Q')
  92.         {
  93.         KEY_QUIT = 1;
  94.         exit(10);
  95.         break;
  96.         }
  97.     }
  98. }
  99.  
  100.  
  101.  
  102. int main(void)
  103. {
  104.    int  sockfd, new_fd; // listen on sock_fd, new connection on new_fd
  105.    struct sockaddr_in   my_addr; // my address information
  106.    struct sockaddr_in   their_addr; // connector’s address information
  107.    int sin_size;
  108.  
  109.    if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
  110.       perror("socket");
  111.       exit(1);
  112.    }
  113.  
  114.    my_addr.sin_family = AF_INET; // host byte order
  115.    my_addr.sin_port = htons(MYPORT); // short, network byte order
  116.    my_addr.sin_addr.s_addr = INADDR_ANY; // automatically fill with my IP
  117.    memset(&(my_addr.sin_zero), 0, 8); // zero the rest of the struct
  118.  
  119.   if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1) {
  120.       perror("bind");
  121.       exit(1);
  122.   }
  123.   if (listen(sockfd, BACKLOG) == -1) {
  124.       perror("listen");
  125.       exit(1);
  126.   }
  127.  
  128.   while(1) {                     // main accept() loop
  129.  
  130.       printf("Wait for connection....\n");
  131.  
  132.       sin_size = sizeof(struct sockaddr_in);
  133.       if ((new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size)) == -1) {
  134.           perror("accept");
  135.           continue;
  136.       }
  137.  
  138.       printf("Handle Connection...\n");
  139.       HandleConnect(new_fd);
  140.  
  141.  
  142.       close(new_fd);
  143.  
  144.    } // end of while
  145.    return 0;
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement