简体   繁体   中英

How long does TidTCPClient keep connection status with TidTCPServer without packet communication

I am making a Server-Client communication system using Indy TCP components in Delphi.

Once the client and server component are connected, if for a long time there is no packet communication, how long can this connection be kept open?

If my idea is wrong, then is there any signal packet communication needed to for prevent a disconnection?

At the TCP level, there is no requirement to send packets once the connection is established. If you don't have any messages to send, you don't have to send anything. In theory, the connection can be left open indefinitely.

However, in practice, there may be requirements at the networking level that affect idle connections (firewall/router rules, etc). To address that, client and/or server should periodically tell the other party that the connection is intentionally being left open during periods of idleness (which is just good communication design anyway).

You can add explicit messages to your communication protocol, that is a just a matter of additional coding on your part to send messages back and forth on a timer as needed. If you send a message, and don't get a response back in a reasonable amount of time, you can close the connection on both ends.

If your protocol can't support adding such messages at the application level, you can alternatively enable TCP's own built-in keep-alive packets at the TCP level instead. In this case, either party (or preferably both) can call the TIdTCPConnection.Socket.Binding.SetKeepAliveValues() method after the connection has been established:

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

Where AEnabled turns keep-alives on/off, ATimeMS is the idle timeout in milliseconds before the 1st keep-alive packet is sent, and AInterval is the interval in milliseconds between each subsequent keep-alive packet being sent.

Just like any other TCP packets, keep-alives are ACK'ed at the TCP level. So, if the connection goes stale and keep-alives are not responded to, the OS will timeout after awhile and close the TCP connection for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM