【问题标题】:I want to have persistent TCP connections with a POCO HTTP server我想与 POCO HTTP 服务器建立持久的 TCP 连接
【发布时间】:2013-01-15 21:41:03
【问题描述】:

我正在使用 POCO C++ lib 版本 1.4.3 来实现 HTTP 服务器。 在我的用例中,只有两个或三个客户端,我希望有持久连接。客户端使用 put 请求发送数据,服务器使用“HTTP/1.1 201 Created”回答。客户端打开多个连接。其中一个客户端可以同时打开 20 个连接。

我在 HTTPServerParams 和 Poco::Net::ServerSocket (myPort) 中使用默认值 我正在使用 Poco::ThreadPool (16,48)。

客户端发送http put请求,服务器回复:

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;

【问题讨论】:

    标签: c++ connection poco persistent httpserver


    【解决方案1】:

    您是否尝试过对HTTPServerParams class 的实例使用setKeepAlive(true); 成员函数调用?

    顺便看看setKeepAliveTimeout()同类的成员函数。

    更新

    我发现了一些有趣的事情:如果使用sendBuffer() 函数发送响应,那么响应包含Connection: Keep-Alive 值;但是当使用send() 函数时,响应包含Connection: Close 值。

    所以,有趣的实现细节在这里:poco/Net/src/HTTPServerResponseImpl.cpp。查看send()sendBuffer() 成员函数的实现。

    【讨论】:

    • 奇怪的行为。您能否在问题中添加一些源代码?
    • 感谢您的回复。很抱歉我的回答延迟了很长时间。现在,我在我的问题中添加了源代码
    • 能否将用于写入输出(即send()sendBuffer() 调用)的代码添加到HTTPServerResponse 类的实例中?
    • 我使用了send(),然后我阅读了您的评论,现在我使用的是sendBuffer("",0)。明天,我必须检查结果。
    猜你喜欢
    • 1970-01-01
    • 2014-09-12
    • 1970-01-01
    • 2010-12-01
    • 2012-07-30
    • 1970-01-01
    • 1970-01-01
    • 2011-10-26
    • 1970-01-01
    相关资源
    最近更新 更多