0

クラス用の HTTP サーバーの作成に取り組んでいます。しかし、返されたファイルは Firefox で表示されません。応答ヘッダーを作成し、送信するファイルの右側content-lengthに配置してから、 への呼び出しで応答ヘッダーを送信しますsend()。次にsend()、ファイルを送信するためにもう一度使用しますが、Firefox でファイルが表示されないのはなぜですか? 以下に関連するコードを掲載します。

error_responseコードを見るとわかるように、404 エラーで ,を送信できません。ライブ http ヘッダーを使用すると、Firefox が正しい応答ヘッダーを受信し、正しい応答を出力することがわかりますcout << error_response << endl;。Firefox で表示されないのはなぜですか? send()ヘッダーと応答パケットを含む呼び出しを 1 回だけ行う必要がありますか?

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

1 に答える 1

0

同じ送信コマンドで応答ヘッダーとファイルを送信しようとしましたか? firefox が何を受け取っているかを知る方法はありますか。HTTPRequester クラスを使用して、HTTP 要求をサーバーに送信するコンソール アプリを作成し、何が返されるかを確認できます。

于 2009-11-26T19:30:03.257 に答える