Indy idHttp freezes - How to work with keep-alive?

I developed a Webserver that uses idHttpServer and a client application that uses idHTTP.

I am using Delphi 2010 and the latest indy svn source from trunk.

This application sends about 1000 requests to the Web Server in a loop. Because of TIME_WAITS and the overhead of connecting to a webserver, I need to use keep-alive. The problem is: after making about 700 requests to the server, my application (the client side) hangs for almost 10 minutes when posting data to the webserver (that happens almost every time).

So, I need to know how to properly use keep-alive with indy.

So far I have this code:

On the client side:

oIndyHttpClient := TIdHTTP.Create(nil);
oIndyHttpClient.Request.CacheControl := 'no-cache';
oIndyHttpClient.ProtocolVersion := pv1_1;
oIndyHttpClient.HTTPOptions := oIndyHttpClient.HTTPOptions + [hoKeepOrigProtocol];
oIndyHttpClient.ReuseSocket := rsOSDependent;
oIndyHttpClient.Request.Connection := 'keep-alive';

And on the server side:

oIdHttpServer.OnCommandGet := Self.OnClientRead;
oIdHttpServer.AutoStartSession := False;
oIdHttpServer.KeepAlive := False;

procedure TPLKWSServerSocketIndy.OnClientRead(AContext: TIdContext; ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
  //do some stuff here
  if LowerCase(ARequestInfo.Connection) = 'keep-alive' then begin
    AResponseInfo.CloseConnection := False;
  else begin
    AResponseInfo.CloseConnection := True;

Am i doing it right? What can be causing the client application do freeze and do not complete the post request?

I tried do debug the server when the client freezes, but the OnClientReadmethod does not get fired. It seems to me that the client is having issues trying to connect do the web server.

If I modify the client code to:

oIndyHttpClient.ProtocolVersion := pv1_0;
oIndyHttpClient.Request.Connection := 'close';

The client app does not freeze and everything works nice.

Should I clear IOHandler.InputBuffer before sending a request to the server? Is there anything else I need to do?



You do not need to manage keep-alives manually on the server side. TIdHTTPServer handles that for you. Simply set the TIdHTTPServer.KeepAlive property to True (it is False by default, and your code is setting it to False anyway) and do not set the AResponseInfo.CloseConnection property at all. TIdHTTPServer decides what value to set it to, on a per-request basis, before firing the OnCommandGet event.

Need Your Help

how to keep browser mode unchanged in IE9 php

php javascript html internet-explorer codeigniter

hi i have a trouble when using my site in IE9 . the thing is

Amazon S3 Python S3Boto 403 Forbidden When Signature Has '+' sign

python django amazon-s3 amazon-web-services boto

I am using Django and S3Boto and whenever a signature has a '+' sign in it, I get a 403 Forbidden. If there is no '+' sign in the signature, I get the resource just fine. What could be wrong here?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.