For the purpose of unit testing your KeepAlive code, you may want to connect to your server’s listening port using telnet as the client. You can do this in one of the following ways:
You can run telnet on the same machine as the server as follows:
$ telnet 127.0.0.1 SERVERPORT
You can run telnet on a different machine from the server as follows: (Note that SERVER can be either the server name or the server IP address.)
$ telnet SERVERSERVERPORT
After the connection is established, you can see whether KeepAlive is enabled by running netstat on the server machine as follows:
$ netstat -a -n -o | grep -w :SERVERPORT
tcp 0 0 0.0.0.0:SERVERPORT 0.0.0.0:* LISTEN off (0.00/0/0)
tcp 0 0 CLIENTIP:CLIENTPORTSERVERIP:SERVERPORT ESTABLISHED off (0.00/0/0)
tcp 0 0 SERVERIP:SERVERPORTCLIENTIP:CLIENTPORT ESTABLISHED keepalive (897.46/0/0)
The output of netstat shows you 2 or 3 lines which are explained as follows:
The LISTEN line is your server program listening on the port SERVERPORT again after accepting the first client connection.
The ESTABLISHED off line is the client-side of the socket connection (which most likely has KeepAlive switched off). [This line will only be present if the client is running on the same machine as the server.]
The ESTABLISHED keepalive line is the server-side of the socket connection (which has KeepAlive switched on if your server KeepAlive code is working correctly). The first number in brackets, which in this example is 897.46, is the number of seconds left before a KeepAlive probe will be sent. If you repeatedly run the above netstat command, you will see this number getting smaller and smaller.
SERVERIP is the server machine’s IP address.
SERVERPORT is the port being listened to on the server machine.
CLIENTIP is the client machine’s IP address.
CLIENTPORT is the port being used on the client machine. This is an
ephemeral port.