Monday, November 4, 2013

TCP Connection States with an Example

Often I tend to get confused with the TCP states showing up in the output of netstat -an. I have gone through the theoretical explanation a number of times. One of the good references I came across is from Microsoft Support knowledge base article at-

http://support.microsoft.com/kb/137984

However, over time, i have learnt that unless you couple theory with hands on, it is very difficult to remember the things.

Following is an example of how the connection states changed when I installed Redis master and slave instances in my host and brought up and shutdown the slave instance.

Note that my redis master instance was configured to run on port 6379 while the slave was configured to run on 6389 (that is, clients could connect with redis instances on these ports).
  1. Initial netstat -an Output (before slave is brought up)$ netstat -an | grep 6379
    tcp        0      0 0.0.0.0:6379                0.0.0.0:*                   LISTEN
    • This indicates that my master redis instance is running on port 6379 and is is ready to accept connection (indicated by connection state LISTEN).
  2.  After Slave is Started$ netstat -an | grep 6379
    tcp        0      0 0.0.0.0:6379                0.0.0.0:*                   LISTEN     
    tcp        0      0 127.0.0.1:6379              127.0.0.1:53525             ESTABLISHED
    tcp        0      0 127.0.0.1:53525             127.0.0.1:6379              ESTABLISHED
    • This indicates that the server has established a connection with the slave (running on the same host) on port 53525 (indicated by connection state ESTABLISHED).
    •  Since, both master and slave are located on the same host, we also see another line (third one in the output) that says that the slave has established a connection with the master on the port 53525.
    • The first line in the output indicates that the master is ready to accept more connections.
  3.  After Slave is Shutdown