2009-11-26 78 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

相關問題