我想与POCO HTTP服务器建立持久的TCP连接

Jue*_* M. 3 c++ connection poco persistent httpserver

我正在使用POCO C ++ lib版本1.4.3来实现HTTP服务器。在我的用例中,只有两个或三个客户端,我想拥有持久的连接。客户端发送带有放置请求的数据,服务器使用“ HTTP / 1.1 201 Created”(HTTP / 1.1 201已创建)进行应答。客户端打开多个连接。其中一个客户端可以同时打开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
Run Code Online (Sandbox Code Playgroud)

我在带有WireShark的PCAP文件中看到了此结果。

如果我不希望服务器在放置请求后关闭连接,该怎么办?

---编辑并插入源代码:

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 ...
}
}
Run Code Online (Sandbox Code Playgroud)

HTTPServer类的私有成员:

 uint16_t                                m_port;
 bool                                    m_wasStarted;
 std::auto_ptr<Poco::ThreadPool>         m_threadPool;
 std::auto_ptr<Poco::Net::HTTPServer>    m_server;
Run Code Online (Sandbox Code Playgroud)

Ser*_*nov 5

您是否尝试过将setKeepAlive(true);成员函数调用用于HTTPServerParams类的实例?

顺便说一下,看看setKeepAliveTimeout()同一个类的成员函数。

更新资料

我发现了一些有趣的东西:如果使用该sendBuffer()函数发送响应,则响应中包含Connection:Keep-Alive值;但是当使用send()函数时,响应包含“ 连接:关闭”值。

因此,这里有趣的实现细节:poco / Net / src / HTTPServerResponseImpl.cpp。请参见send()sendBuffer()成员函数的实现。