TCP Keep Alive on idHttpServer server (server) and wininet (client)

I have a web server application developed using idHttpServer. When the client connects, my web server and, for some unknown reason, got disconnected (not elegantly disconnected), my web server does not receive notifications. I know this is normal behavior, but I need to know when a client dies.

There are several ways to do this. I know two good ways:

1 - Implement the heart rate mechanism. The client socket notifies the server that it is still alive (it needs some work and some code to make it work)

2 - TCP Keep Alive . I like it the most, because it doesn’t require too much code and work. But I have a few questions regarding this.

  • Can this be used with idHttpServer (for server) and wininet (for client)?
  • Does this really work as expected? I mean, the server is not allowed when the client dies all the time?
  • Does anyone have a sample setup on wininet? (I think it should be installed on the client side, right?)
  • Is there a better way to notify the server that the client is dead (using indy and wininet, of course)

EDIT

I am using Delphi 2010 and the latest Indy10 source code from their svn.

+3
source share
1 answer

If you are using an updated version of Indy 10, you can use the method TIdSocketHandle.SetKeepAliveValues():

procedure SetKeepAliveValues(const AEnabled: Boolean; const ATimeMS, AInterval: Integer);

For instance:

procedure TForm1.IdHTTPServer1Connect(AContext: TIdContext);
begin
  // send a keep-alive every 1 second after
  // 5 seconds of inactivity has been detected
  AContext.Binding.SetKeepAliveValues(True, 5000, 1000);
end;

Please note that ATimeMSand options AIntervalare only supported in Win2K and later.

TIdSocketHandle.SetSockOpt(), TCP/IP SO_KEEPALIVE :

procedure TIdSocketHandle.SetSockOpt(ALevel: TIdSocketOptionLevel; AOptName: TIdSocketOption; AOptVal: Integer);

:

procedure TForm1.IdHTTPServer1Connect(AContext: TIdContext);
begin
  AContext.Binding.SetSockOpt(Id_SOL_SOCKET, Id_SO_KEEPALIVE, 1);
end;
+2

All Articles