Sets or gets the Boolean flag that controls whether a keep-alive message is automatically transmitted over the current TCP connection.
socket_object.KeepAlive= <boolean_value>
-or-
...socket_object.KeepAlive
Prerequisites
The Socket must currently be open to set this property.
Parameters
None
Remarks
This property sets, clears or returns the keep-alive flag for the current TCP connection. When set, the local network node sends a special keep-alive packet periodically on the TCP connection whenever it is idle for a period of time. This message permits the system to detect if the network connection is broken (e.g. the network cable is unplugged) even if the associated GPL thread has not recently communicated using the connection.
If this flag is not set, an idle TCP connection does not send any messages. If the network path is broken, the local node will not detect the broken connection until it attempts to send a message.
Using the keep-alive feature eliminates the need to implement “heartbeat” messages within your application to detect broken connections. Also, since the keep-alive message is only sent when the connection is idle, it does not increase traffic on a busy connection.
The keep-alive timing for GPL is pre-set as described below and cannot be changed.
- If the connection is idle, a keep-alive packet is sent every 14 seconds.
- If no response is received, additional keep-alive packets are sent every 2 seconds.
- If no response is received after 9 successive keep-alive packets (a total of 32 seconds) the connection is closed locally.
The keep-alive flag only enables the local node to detect a broken connection. If the remote node wishes to detect a broken connection, it must also set its keep-alive flag.
Examples
Dim tc As New TcpClient() ' Optional endpoint not specified
Dim sock As Socket
Dim ep As New IPEndPoint("192.168.0.3", 1234)
sock = tc.Client
sock.Connect(ep)
sock.KeepAlive = True ' Enable keep-alive for this connection
See Also