I want to have persistent TCP connections to the POCO HTTP server
I am using POCO C ++ lib version 1.4.3 to implement an HTTP server. In my use case, there are only two or three clients and I would like to have persistent connections. Clients send data with send requests, and the server responds with "HTTP / 1.1 201 Created". Clients open multiple connections. One of the clients could open 20 connections at the same time.
I am using the defaults in HTTPServerParams and Poco :: Net :: ServerSocket (myPort) and I am using Poco :: ThreadPool (16,48).
The client sends an http put request and responds to the server:
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 using WireShark.
What should I do if I don't want the server to close the connection after the request is requested?
--- edit and paste the 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 HTTPServer class:
uint16_t m_port;
bool m_wasStarted;
std::auto_ptr<Poco::ThreadPool> m_threadPool;
std::auto_ptr<Poco::Net::HTTPServer> m_server;
source to share
Have you tried using a function call setKeepAlive(true);
on an instance of the HTTPServerParams class ?
By the way, take a look at the member function of setKeepAliveTimeout()
the same class.
Update
I found out something interesting: if a function is sendBuffer()
used to send a response, then the response contains the value Connection: Keep-Alive; but when the function is used send()
, the response contains the Connection: Close value.
So, interesting implementation information is here: poco / Net / src / HTTPServerResponseImpl.cpp . See Implementing Member Functions send()
and sendBuffer()
.
source to share