Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/socket.h>
- #include <sys/types.h>
- #include <arpa/inet.h>
- #include <netinet/in.h>
- #include <errno.h>
- #include <netdb.h>
- #include <unistd.h>
- #include <ctype.h>
- #define MYPORT 12345
- #define BACKLOG 5
- int KEY_QUIT = 0;
- static char copybuf[16384]={0};
- #define DEBUG 1
- int copy(FILE *read_f, FILE * write_f) //複製html文件
- {
- int n;
- int wrote;
- n = fread(copybuf,1,sizeof(copybuf),read_f);
- wrote = fwrite(copybuf,n,1,write_f);
- return 0;
- }
- int DoHTML(FILE *f, char * name)
- {
- FILE *infile;
- infile = fopen(name,"r"); //打開Client端請求的html文件
- printf("Sending mesg...\n");
- copy(infile,f); //複製Client客戶端請求的html文件
- printf("Complete sending.\n");
- fgets(copybuf,1000,infile);
- printf("\n");
- puts(copybuf);
- printf("\n");
- fread(copybuf,1,sizeof(copybuf),f);
- printf("DoHTML->fread done");
- fclose(infile);
- return 0;
- }
- int ParseReq(FILE *f, char *r) //分析當中的內容,提取client請求的文件名
- {
- char *bp;
- char *c;
- #ifdef DEBUG
- #endif
- while(*(++r) != ' '); //去掉請求內容開頭的空格部分
- while(isspace(*r)) r++; //判斷輸入字符是否為空格/換行等,去掉所有這些字符
- while(*r == '/') r++; //去掉所有的'/'字符
- bp = r; //得到文件名的起始
- while(*r && (*r != ' ') && (*r != '?')) r++;
- *r = 0;
- c = bp;
- printf("The Request FileName is %s\n", c);
- DoHTML(f, c); //得到文件名之後處理請求
- return 0;
- }
- int HandleConnect(int fd)
- {
- FILE *f;
- char buf[160];
- f = fdopen(fd, "a+"); //打開對應文件名的html文件
- setbuf(f, 0); //清除f文件緩存
- fgets(buf, 150, f); //從Client請求中獲取前面150個字符
- #ifdef DEBUG
- #endif
- ParseReq(f, buf); //分析請求的內容
- printf("ParseReq done");
- fflush(f);
- printf("free success");
- fclose(f);
- return 1;
- }
- void * key(void *data)
- {
- int c;
- for(;;)
- {
- c = getchar();
- if(c == 'q'||c == 'Q')
- {
- KEY_QUIT = 1;
- exit(10);
- break;
- }
- }
- }
- int main(void)
- {
- int sockfd, new_fd; // listen on sock_fd, new connection on new_fd
- struct sockaddr_in my_addr; // my address information
- struct sockaddr_in their_addr; // connector’s address information
- int sin_size;
- if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
- perror("socket");
- exit(1);
- }
- my_addr.sin_family = AF_INET; // host byte order
- my_addr.sin_port = htons(MYPORT); // short, network byte order
- my_addr.sin_addr.s_addr = INADDR_ANY; // automatically fill with my IP
- memset(&(my_addr.sin_zero), 0, 8); // zero the rest of the struct
- if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1) {
- perror("bind");
- exit(1);
- }
- if (listen(sockfd, BACKLOG) == -1) {
- perror("listen");
- exit(1);
- }
- while(1) { // main accept() loop
- printf("Wait for connection....\n");
- sin_size = sizeof(struct sockaddr_in);
- if ((new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size)) == -1) {
- perror("accept");
- continue;
- }
- printf("Handle Connection...\n");
- HandleConnect(new_fd);
- close(new_fd);
- } // end of while
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement