4

HTTPサーバーを実装するためにPOCOC++libバージョン1.4.3を使用しています。私のユースケースでは、クライアントは2つか3つしかないので、持続的接続が必要です。クライアントはputリクエストでデータを送信し、サーバーは「HTTP /1.1201Created」で応答します。クライアントは複数の接続を開きます。クライアントの1つは、同時に20の接続を開くことができます。

HTTPServerParamsとPoco::Net :: ServerSocket(myPort)のデフォルトを使用し、Poco :: ThreadPool(16,48)を使用しています。

クライアントはhttpputリクエストを送信し、サーバーは次のように応答します。

Server:
HTTP/1.1 201 Created
Connection: Close
Date: Thu, 31 Jan 2013 15:21:50 GMT

この結果は、WireSharkを使用したPCAPファイルで確認できました。

putリクエスト後にサーバーが接続を閉じたくない場合はどうすればよいですか?

---ソースコードを編集して挿入します:

HTTPServer::HTTPServer(uint16_t port, 
                       Poco::Net::HTTPRequestHandlerFactory* handlerFactory):
m_port(port),
m_wasStarted(false)
{
    // HTTPServer takes ownership
Poco::Net::HTTPServerParams*   options = new Poco::Net::HTTPServerParams; 

try
{
    m_threadPool.reset(new Poco::ThreadPool (16,48));

    std::cout << "HTTPServerParams.keepAlive: " 
                  << options->getKeepAlive() << std::endl;

    std::cout << "HTTPServerParams.keepAliveTimeout: " 
                  << options->getKeepAliveTimeout().totalSeconds() << std::endl;

    std::cout << "HTTPServerParams.MaxKeepAliveRequests: " 
                  << options->getMaxKeepAliveRequests()<< std::endl;

    std::cout << "HTTPServerParams.Timeout: " 
                  << options->getTimeout().totalSeconds() << std::endl;

    m_server.reset(new Poco::Net::HTTPServer(handlerFactory,  
                                                 *m_threadPool,
                                                 Poco::Net::ServerSocket(m_port),
                                                                         options)
                                                );
}
catch (const Poco::Exception& e)
{
       //some code ...
}
}

クラスHTTPServerのプライベートメンバー:

 uint16_t                                m_port;
 bool                                    m_wasStarted;
 std::auto_ptr<Poco::ThreadPool>         m_threadPool;
 std::auto_ptr<Poco::Net::HTTPServer>    m_server;
4

1 に答える 1

6

classsetKeepAlive(true);のインスタンスに対してメンバー関数呼び出しを使用しようとしましたか?HTTPServerParams

ところで、setKeepAliveTimeout()同じクラスのメンバー関数を見てみましょう。

アップデート

興味深いことがわかりました。sendBuffer()関数を使用して応答を送信すると、応答にConnection: Keep-Alive値が含まれます。ただし、send()関数を使用すると、応答にConnection: Close値が含まれます。

興味深い実装の詳細はこちら: poco/Net/src/HTTPServerResponseImpl.cpp . send()およびsendBuffer()メンバー関数の実装を参照してください。

于 2013-01-31T17:38:28.463 に答える