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

问题描述 投票:3回答:1

我正在使用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个回答
5
投票

你试过使用setKeepAlive(true); 成员函数调用HTTPServerParams类的实例?

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

更新

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

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

© www.soinside.com 2019 - 2024. All rights reserved.