1

I'm investigation on a growing of active TCP connection.

Seems TIdTCPClient.Disconnect don't close connection.

This is a sample project

program Project2;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  System.SysUtils,
  IdTCPClient;

var
  FClient: TIdTCPClient;
begin
  try
    FClient := TIdTCPClient.Create();
    FClient.Connect('LOCALHOST', 6379);

    FClient.Disconnect;
    FClient.Free;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

Opening this console application multiple times cause a growing of the connection

netstat -na | find "6379"

enter image description here

Side note: I'm on Berlin 10, Windows 7 (but is the same on Windows 10)

ar099968
  • 6,963
  • 12
  • 64
  • 127

2 Answers2

6

Disconnect() is closing the connection. The TIME_WAIT state is normal TCP behavior.

Whichever peer is first to actively close the TCP connection (in this case, your client), its socket endpoint goes into the TIME_WAIT state, which is a safety feature of TCP to discard any stray packets remaining in that connection. The endpoint will be fully released after a few moments once TIME_WAIT times out.

On the other peer, the one passively receiving notification of the closure, its socket endpoint goes into the CLOSE_WAIT state instead, and is released once the closure is ACKed by the other peer. There is no TIME_WAIT on that side.

See TIME_WAIT and its design implications for protocols and scalable client server systems, which goes into a very lengthy discussion of what TIME_WAIT is, why it exists, and how to work with it effectively.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
1

The connection is closed. TIME_WAIT indicates that local endpoint (this side) has closed the connection.

Ref: https://superuser.com/questions/173535/what-are-close-wait-and-time-wait-states

fpiette
  • 11,983
  • 1
  • 24
  • 46
  • "*The other side has not yet acknowledged the closing*" - that is not what TIME_WAIT means. See [this answer](https://stackoverflow.com/a/33178079/65863). In fact, you can't get into `TIME_WAIT` *until* the other side has acknowledged the close. – Remy Lebeau May 04 '21 at 15:20