2009-11-26 41 views
0

我正在为我的类制作HTTP服务器。但是,返回的文件没有被Firefox显示。我构建了一个响应头文件,并将其放在我想发送的文件的正确content-length中,然后通过调用send()来发送响应头文件。接下来我再次使用send()发送文件,为什么Firefox不显示该文件?我将在下面发布相关代码。在我的http服务器中数据不会与http响应一起发送

正如您将看到的代码一样,在404错误期间,我无法发送error_response。使用实时HTTP标头,我可以看到Firefox收到正确的响应标题,并且cout << error_response << endl;确实打印出正确的响应。为什么Firefox不显示它?我是否只需要对包含头和响应包的send()进行一次呼叫?

// 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; 
} 

回答

0

你试过发送响应头文件在同一send命令? 有没有一种方法可以告诉firefox正在接收什么。您可以尝试使用一些HTTPRequester类来编写一个控制台应用程序,该应用程序向您的服务器发送一个HTTP请求,并查看返回的结果。

+0

我可以试试看,我认为它可能会起作用,但是这样做的要求吗?如果文件太大而不能完全缓冲到内存中,并且您必须执行多次发送? – adhanlon 2009-11-27 03:59:21