Search code examples
c++connectionpocopersistenthttpserver

I want to have persistent TCP connections with a POCO HTTP server


I am using POCO C++ lib version 1.4.3 in order to implement an HTTP server. In my use-case there are only two or three clients and I would like to have persistent connections. The clients send data with put requests and the server answers with "HTTP/1.1 201 Created". The clients open more than one connection. One of the clients could open 20 connections at the same time.

I use the defaults in HTTPServerParams and Poco::Net::ServerSocket (myPort) and I am using a Poco::ThreadPool (16,48).

The clients sends a http put request and the server answers:

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

I saw this result in a PCAP-File with WireShark.

What do I have to do if I don't want that the server closes the connection after a put request?

--- edit and insert source code :

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 ...
}
}

private member from class HTTPServer:

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

Solution

  • Have you tried to use setKeepAlive(true); member function call for the instance of HTTPServerParams class?

    By the way, take a look at setKeepAliveTimeout() member function of the same class.

    Update

    I have found out something interesting: if the sendBuffer() function is used to send the response, then the response contains the Connection: Keep-Alive value; but when the send() function is used, the response contains the Connection: Close value.

    So, the interesting implementation details here: poco/Net/src/HTTPServerResponseImpl.cpp. See implementation of send() and sendBuffer() member functions.