webサーバを実装しており, セッションステートレスにする方法がわからなかったので質問いたしました
ソケットを開いたのちコネクションのループに入ります
HTTP1.1のkeep-alive をちゃんと送り返しているし, select を用いてタイムアウト処理をしているのになぜか上手くいきません
一回のリクエストで3度タイムアウトが起こってしまいます.
またプロセスが3つできていることからなにかクライアント側の処理による可能性があります
しかしクライアント側はchromeを使っているため原因がわかりません
ソースコードのアドバイスをお願いいたします
C
1 2int 3client_proc(int acc) 4{ 5 char buf[HTTP_B], meth_name[16]; 6 char uri_addr[HTTP_P]; 7 char http_ver[64], content_type[HTTP_P]; 8 char header_type[64], host[HTTP_P], connect[HTTP_P]; 9 char *uri_file; 10 int read_fd, len, p_num = 0; 11 char *p, *media; 12 struct timeval timeout; 13 fd_set mask, ready; 14 int end, width; 15 16 /*select 用のマスク&デスクリプタ*/ 17 FD_ZERO(&mask); 18 FD_SET(acc, &mask); 19 width = acc + 1; 20 21 for(;;){ 22 ready = mask; 23 timeout.tv_sec = 1; 24 timeout.tv_usec = 0; 25 switch (select(width,(fd_set *) &ready,NULL,NULL,&timeout)){ 26 case -1: 27 perror("select"); 28 break; 29 case 0: 30 fprintf(stderr,"timeout\n"); 31 return 0; 32 default: 33 if(FD_ISSET(acc, &ready)) { 34 if(recv(acc, buf, HTTP_B, 0) <= 0) { 35 perror("read"); 36 }else { 37 p = buf; 38 sscanf(buf, "%s %s %s",meth_name,uri_addr,http_ver); 39 40 fprintf(stderr,"%s",p); 41 while(*p != '\n') 42 p++; 43 p++; 44 sscanf(p,"%s %s",header_type, host); 45 fprintf(stderr,"HOST = %s\n", host); 46 47 while(*p != '\n') 48 p++; 49 p++; 50 51 sscanf(p,"%s %s",header_type, connect); 52 fprintf(stderr,"Connect = %s\n", connect); 53 if(strcmp(connect, "keep-alive") == 0){ 54 fprintf(stderr,"KEEP CONNECTION\n"); 55 } 56 57 /*メディアタイプ*/ 58 media = uri_addr; 59 while(*media != '.') 60 media++; 61 media++; 62 fprintf(stderr,"MEDIA = %s\n",media); 63 /*example 64 meth_name: "GET" 65 uri_addr: "/index.html" 66 http_ver: "HTTP/1.0" 67 */ 68 if(strcmp(media, "html") == 0){ 69 sprintf(content_type,"Content-Type: text/html; charset=UTF-8\r\n"); 70 }else if(strcmp(media, "jpeg") == 0){ 71 sprintf(content_type,"Content-Type: image/jpeg\r\n"); 72 }else if(strcmp(media, "mp3") == 0){ 73 sprintf(content_type,"Content-Type: audio/mpeg\r\n"); 74 } 75 76 if(strcmp(meth_name,"GET") != 0){ 77 send_msg(acc,"HTTP/1.1 501 Not Implemented\r\n"); 78 }else{ 79 /*先頭のスラッシュをとることでファイル置き場を実行ディレクトリにする*/ 80 uri_file = uri_addr + 1; 81 if((read_fd = open(uri_file, O_RDONLY,0666)) == -1) { 82 send_msg(acc,"HTTP/1.1 404 Not Found\r\n"); 83 }else{ 84 send_msg(acc, "HTTP/1.1 200 OK\r\n"); 85 send_msg(acc, "Date: \r\n"); 86 send_msg(acc, "Server: myhttpd\r\n"); 87 send_msg(acc, "Connection: keep-alive\r\n"); 88 send_msg(acc, content_type); 89 send_msg(acc,"\r\n"); 90 while((len = read(read_fd, buf, HTTP_B)) > 0) { 91 if(send(acc, buf, len, 0) != len) { 92 perror("send"); 93 break; 94 } 95 } 96 close(read_fd); 97 } 98 } 99 } 100 } 101 } 102 } 103 return 0; 104} 105 106int send_msg(int acc, char *msg){ 107 int len; 108 len = strlen(msg); 109 if(send(acc, msg, len, 0) != len) 110 perror("send"); 111 112 return len; 113}
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。