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

Sunday, March 2, 2008

Java 1.5: Generics and Collections Notes- Collections



  1. Following are the important interfaces from Collections Framework- Collection, Set, SortedSet, List, Map, SortedMap, Queue



  2. Following are the important classes from Collections Framework- HashMap, Hashtable, TreeMap, LinkedHashMap, HashSet, LinkedHashSet, TreeSet, ArrayList, Vector, LinkedList, PriorityQueue, Collections, Arrays



  3. Note the distinction between Collection interface and Collections class. While former is the base or root interface for many other Collections Framework interfaces and classes like Set, List, Queue, the latter is a utility class containing a set of static utility methods.



  4. Note also that there is another (interface apart from Collection) Map which is base (or root) interface for other intreface and classes like HashTable, LinkedHashMap, HashMap, SortedMap, TreeMap.



  5. There are two utility classes in the Collections framework- Arrays and Collections, both of which extend from the Object class.



  6. Collections can be ordered or unordered and sorted or unsorted (though, with one exception, given next). A given collection can be either unordered and unsorted or ordered and sorted or ordered but unsorted. A collection can never be sorted but unordered.


  7. When a collection is ordered, it means you can iterate through the collection in a specific (non-random) order.


  8. When a collection is sorted, it means that the order in the collection is determined according to some rule or rules, known as the sort order.


  9. List interface maintians collections using indices. There are three implementations of List in the collections framework- ArrayList, Vector and LinkedList. ArrayList (or Vector) are preferred when faster iteration is required. LinkedList is preferred when fast insertion or deletion is required.


  10. Set interface in meant for holding collection of objects which does not have meaningful duplicates. The three implementation classes of Set interface are- HashSet (which is unordered and uses hashcodes for insertion and retrieval), LinkedHashSet (which maintains a doubly linked list across all elements of the set (so is ordered) ) and TreeSet (which is a set sorted by natural order of the objects)


  11. Map interface uses unique identifiers to store objects in the collection. There are three implementations of Map- HashMap (which is unordered and uses hashCode() of the key object to determine the value object's storage location), Hashtable (which is similar to HashMap but its method's are synchronized), LinkedHashMap (which maintains the insertion order of the objects) and TreeMap (which sorts kbjects by their natural sort order).


  12. HashMap and Hashtable have another difference (apart from Hashtable having synchronized methods). HashMap allows one null key and multiple null values, while Hashtable does not.


  13. Queue interface is designed to hold a list of things to do. The typical order in which objects are stored in a Queue is FIFO. Queue has one implementation class (apart from LinkedList)- PriorityQueue in which objects are ordered by their natural ordering. A Comparator interface can be used to specify custom ordering. In any case, the elements' ordering represents their relative priority.

  14. A LinkedList has faster (higher) addition-removal performance compared to a ArrayList. But it is slower when it comes to iteration. On the other hand, a LinkedHashMap has slower (lower) addition-removal performnance but faster iteration rate when compared with with a HashMap.

  15. Collections.sort() (one-arg) method takes an argument which is of List type and the objects in the List must implement an interface called Comparable.

  16. For implementing Comparable interface, a class has to implement the method compareTo(). This method (taking advantage of Generics in Java 5) takes an argument of the class type which is being compared.

  17. To sort classes in multiple different ways and to sort classes which you can not modify, one has to use Comparator interface. To implement Comparator interface, one has to implement the method compare() which takes two arguments of the type of the objects which are to be compared.

  18. Arrays of primitives are always sorted on their natural order.

  19. Arrays.sort() and Collections.sort() are static methods.

  20. Searching of Arrays and Collections is done using binarySearch method, successful search returns int index of the element being searched, unsuccessful search returns an int index that represents the insertion point of the element in the Array or Collection (the actual value returned is (-(insertion point) - 1).

  21. Arrays and collections must be sorted before sorted before it can be searched. It must be searched in the order in which it was sorted.

  22. Arrays.sort() and Collections.sort() methods update the objects being sorted.

  23. Iterators are used to iterate over elements of a Collection. Two important methods of an Iterator are "boolean hasNext()" and "Object next()".

  24. All the elements in a TreeSet and a TreeMap must be mutually comparable.
  25. String, Object and Enum classes override hashCode and equals() methods.
  26. Important methods of Queues interface are offer() (which loads elements in a Queue), peek() (which returns element at the head of the queue without removing it from the Queue), poll() (which returns the element at the head of the queue and removes it from the Queue.

Java 1.5: Generics and Collections- equals() and hashCode()

  1. The default implementation of equals() method from Object class uses == operator for comparison. So if you don't override equals appropriately in your class, you can not use objects of your class as keys in a hashtable, or for building sets of objects such that there are no conceptual duplicates.
  2. If two objects are equal (by virtue of the equals() method returning true for them), their hashcodes (as returned by the method hasCode()) must be equal too. Otherwise, those objects can not be used in a collection which uses hashing (like HashMap or HashSet)
  3. Do not use transient variables in hasCode() and equals() overrides.