2

I want close idle connection on TIdHttpServer.

Here https://stackoverflow.com/a/35107685/2936170 i found

[...] the ReadTimeout property can be used to disconnect slow/dead clients that do > not send requests in a timely manner.

On TIdHttpServer server i have set the ReadTimeout to 1 second on OnConnect event

begin
   IdHTTPServer.KeepAlive        := False;
   IdHTTPServer.OnConnect        := OnConnect;
   IdHTTPServer.DefaultPort      := 80;
   IdHTTPServer.AutoStartSession := False;
   IdHTTPServer.SessionState     := False;

   IdHTTPServer.Active           := True;
end;

procedure OnConnect(AContext: TIdContext);
begin
   AContext.Connection.Socket.ReadTimeout := 1000; // 1 second
end;

For test the server timeout, i have created a simple client and i make a TCP connection with TIdTCPClient

begin
    IdTCPClient.Port := 80;
    IdTCPClient.Host := '127.0.01';
    IdTCPClient.Connect;
end;

On windows network activity section, from resource monitor, i see in the tpc connection list the client and the server connection. Each connections (server and client) persists over the server timeout...

Why IdHttpServer don't close the connection over the readtimeout?

UPDATE 1

tcp connection disappear after 2 minutes (but my timeout is 1 second).

UPDATE 2

if i comment out the line of readtimeout, the connection never disappear. IdHTTPServer seem to have a defaut infinite timeout. I think readtimeout work, but windows network activity is not a real time status info.

Community
  • 1
  • 1
ar099968
  • 6,963
  • 12
  • 64
  • 127
  • 1
    A guess. The purpose of ReadTimeout is not to close stale connections. ReadTimeout is checked while reading data. It is taken in account only if data is available, you have requested that data to be read, but reading fails for some reason. Plus, ReadTimeout should be reset each time a successful byte has been read. And in your case you seems to be expecting that the connection is closed automatically if it has been idle for X seconds? – quasoft Feb 26 '16 at 11:26
  • @quasoft yep, i want simple close idle connection (after X second)... – ar099968 Feb 26 '16 at 11:30
  • May be you should implement that in the server. Make server remember the last time each client connection has sent/received data. Then use a timer to check if X seconds have passed since last message was sent/received. If X seconds have passed, make server close the connection. Although it is all strange. An http server usually does not keep open connections. – quasoft Feb 26 '16 at 11:32
  • @quasoft if i comment out the line of readtimeout, the connection never disappear. IdHTTPServer seem to have a defaut infinite timeout. With this line the idle connection disappear after 2 minutes. I think readtimeout work, but windows network activity is not a real time status info. – ar099968 Feb 26 '16 at 11:36
  • What about keep alive settings. Do you use it? – quasoft Feb 26 '16 at 11:46
  • @quasoft i have update my answer with more details... keep-alive is disabled. thanks for help! – ar099968 Feb 26 '16 at 11:49
  • Don't have running Delphi right now to test it for you, but try making a simple example that demonstrates the problem, and someone will surely help you. – quasoft Feb 26 '16 at 11:53
  • @quasoft my question have all code needed to reproduce it. basically you create two form, on server form drop a idhttpserver component and on client form drop a idtcpclient, and put code in form create event. – ar099968 Feb 26 '16 at 12:02
  • 2
    Hopefully @Remy Lebeau will notice this question and help. Meanwhile take a look at this thread: http://embarcadero.newsgroups.archived.at/public.delphi.internet.winsock/201008/1008014532.html – quasoft Feb 26 '16 at 12:22
  • 1
    @quasoft: When a client connects to `TIdHTTPServer`, it immediately tries to read a request, and since the client is not sending any request then the `ReadTimeout` should take effect and kill the connection. And in fact, when I test the exact scenario described, it works fine for me, the client is dropped (and the `TIdHTTPServer.OnDisconnect` event is fired) after 1 second as expected, not after 2 minutes. – Remy Lebeau Feb 26 '16 at 18:16

0 Answers0