虽然目录中存在文件但未找到该文件

时间:2018-02-10 02:44:57

标签: c ubuntu server shared-memory proxy-server

我正在尝试实现客户端代理服务器。客户端和代理之间的通信与往常一样(HTTP GET)。但代理服务器和服务器之间的通信是通过共享内存完成的。无论客户端请求哪个文件(在本例中为index.html),代理服务器都会将其写入共享内存。服务器从共享内存中读取它并通过HTTP返回它。以下是代理代码:

             //creating shared memory segment
                char c;
                int shmid;
                key_t key;
                char *shm;
               char *s= (char *)malloc (strlen(buffer)+1);
                //naming shared memory segment 5678
                key=5678;
                //creating the segment
                if((shmid =shmget (key,SHMSZ,IPC_CREAT|0666))<0){
                         perror("shmget");
                         exit(1);
                }
                //attaching segment to our dataspace
                if((shm =shmat (shmid,NULL, 0 )) == (char *) -1){
                         perror("shmat");
                         exit(1);
                 }
                //adding data to segment
                s= shm;
               // strcpy(*s,buffer);
               for(int i=0; i<strlen(buffer); i++){
                *s++= buffer[i];
                printf("inside string s %s",s);
}
                *s= NULL;







               //////////////////////////////////////////////
                write(server_fd, "1", sizeof(buffer));  
                fflush(stdout);  


              //waiting for server to write something
              while(*shm!='*')
                    sleep(1);

这是服务器端

           char c;
          int shmid;
          key_t key;
          char *shm;
          char *s= (char *)malloc (strlen(buffer)+1);
          //naming shared memory segment 5678
          key=5678;
          //Locate the segment
          if((shmid =shmget (key,SHMSZ,0666))<0){
               perror("shmget");
               exit(1);
         }
          //attaching the segment
         if((shm =shmat (shmid,NULL,0))==(char *) -1){
               perror("shmat");
               exit(1);

         }
         s =shm;
         printf("File name received is :\n");

         for(s=shm ; *s!=NULL; s++){
               putchar (*s);
         }
         putchar('\n');
         //changing first character of segment to make sure proxy knows that we have read it
         //reading html file
           char mesg[99999], *reqline[3], data_to_send[BYTES], path[99999];
       int rcvd, fd, bytes_read;
           if ( open(s, O_RDONLY)!=-1)    //FILE FOUND// change
                {
                    printf("%s",s);

                    send(c_fd, "HTTP/1.0 200 OK\n\n", 17, 0);
                    while ( (bytes_read=read(fd, data_to_send, BYTES))>0 )
                        write (c_fd, data_to_send, bytes_read);
                }
                else    {write(c_fd, "HTTP/1.0 404 Not Found\n", 23); //FILE NOT FOUND
                                        }

         *shm = '*';

问题是服务器无法以这种方式获取index.html文件。它说没有找到404。但是当我硬编码字符串&#34; index.html&#34;时,它成功地返回了它。

0 个答案:

没有答案