代码之家  ›  专栏  ›  技术社区  ›  adhanlon

在我的http服务器中,数据不随http响应一起发送

  •  0
  • adhanlon  · 技术社区  · 16 年前

    我正在为我的班级制作一个HTTP服务器。但是,Firefox不会显示返回的文件。我构造了一个响应头并将其放在右边 content-length 对于我要发送的文件,然后我发送响应头并调用 send() . 接下来我用 发送() 再次发送文件,为什么Firefox不显示文件?我将在下面发布相关代码。

    error_response ,在404错误期间。使用实时http头,我可以看到Firefox接收到正确的响应头,并且 cout << error_response << endl; 打印正确的响应。为什么Firefox不显示它?我只需要给你打一个电话吗 发送()

    // send response headers
        response->Print( buffer, 2000 );
        if ( send( acc_tcp_sock, buffer, sizeof(buffer), 0 ) < 0 ) {
          cerr << "Unable to send response headers to client." << endl;
          return 5;
        }
    
        // if 200 and GET then send file
        if ( response->Get_code() == 200 && method == "GET" ) {
          // send file
        }
    
        // close file, if it has been opened
        if ( response->Get_code() == 200 ) {
          fclose( returned_file );
        }
        else if ( method == "GET" ) {
          if ( send( acc_tcp_sock, error_response.c_str(), error_response.length(), 0 ) < 0 ) {
            cerr << "Unable to send data to client." << endl;
            return 6;
          }
          cout << error_response << endl;
        }
    
        close( acc_tcp_sock ); // close the connection
      }
    
      return 0;
    }
    
    1 回复  |  直到 13 年前
        1
  •  0
  •   bobber205    16 年前

    您是否尝试发送响应标题 同一个send命令中的文件? 有没有一种方法可以告诉你firefox收到了什么。您可以尝试使用一些HTTPRequester类编写一个控制台应用程序,向您的服务器发送HTTP请求,并查看它得到了什么回报。

    推荐文章