我使用poco写了https服务器。有时,当客户端下载文件时,他失去了连接。在客户端,它看起来像是在等待重新连接,并且在很长一段时间后他都会断开连接。在服务器端连接下降速度更快。我增加了超时,但这没有给出好的结果。
我该怎么做,因为客户端可以正常下载文件到最后?
发送文件的代码:
void DownloadHandler::handleRequest(HTTPServerRequest& request, HTTPServerResponse& response)
{
response.setVersion(HTTPServerResponse::HTTP_1_1);
QString fileName = "filename.avi";
QFile file(fileName);
const int size = 1024*100; char buffer[size] ;
response.setChunkedTransferEncoding(false);
response.setContentType("application/file");
if(!file.open(QIODevice::ReadOnly)){
return;
}
std::streamsize length = file.size(), readBefore = 0, readNow = 0;
response.setContentLength(length);
std::ostream& ostr = response.send();
while(readBefore < length) {
if((readBefore + size) <= length){
readNow = size;
}
else{
readNow = length - readBefore;
}
readBefore += readNow;
file.read(buffer, readNow);
if(ostr){
ostr.write(buffer, readNow);
ostr.flush();
}
else{
app->printStatus("Error number " + QString::number(response.getStatus()), false, false);
break;
}
}
file.close();
}
服务器初始化:
Poco::SharedPtr<PrivateKeyPassphraseHandler> pConsoleHandler = new KeyConsoleHandler(true);
Poco::SharedPtr<InvalidCertificateHandler> pInvalidCertHandler = new ConsoleCertificateHandler(true);
Context::Ptr pContext = new Context(Context::SERVER_USE, "crypto/mycert.pem", "crypto/mycert.pem", "", Context::VERIFY_ONCE, 9, false, "ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH");
pContext->setSessionTimeout(100);
SSLManager::instance().initializeServer(pConsoleHandler, pInvalidCertHandler, pContext);
Poco::Net::HTTPRequestHandlerFactory::Ptr factory (new RequestFactory() );
Poco::Net::SecureServerSocket socket(20000);
socket.setReceiveTimeout(Poco::Timespan(1000, 0));
socket.setSendTimeout(Poco::Timespan(1000, 0));
socket.setKeepAlive(true);
Poco::Net::HTTPServerParams::Ptr params(new Poco::Net::HTTPServerParams() ) ;
params->setKeepAliveTimeout(Poco::Timespan(10, 10));
params->setTimeout(Poco::Timespan(1000, 10));
params->setKeepAlive(true);
params->setServerName("MediaShark:20000");
params->setSoftwareVersion("1.1");
Poco::ThreadPool pool(2, 6, 60, 0);
Poco::Net::HTTPServer server(factory, pool, socket, params);
server.start();
由于