Thursday, July 16, 2015

Socket: java.net.BindException: Address already in use

java.net.BindException: Address already in use
        at java.net.PlainSocketImpl.socketBind(Native Method)
        at java.net.AbstractPlainSocketImpl.bind(AbstractPlainSocketImpl.java:376)
        at java.net.ServerSocket.bind(ServerSocket.java:376)

When you disconnect a socket connection and immediately use the same IP address and port number for a new socket, this exception may throw. This is because when a TCP connection is closed, the connection may remain in a timeout state for a period of time after the connection is closed, also known as the TIME_WAIT state or 2MSL wait state. It may not be possible to bind a socket to the requred socket address if there is a connection in the timeout state involving the socket address or port. However, setting the reuse address of the socket to true before binding allows the socket to be bound even though a previous connection is in a timeout state.

For example, the following code throws the exception.

      ServerSocket serverSocket = new ServerSocket(<port>);

The following code will not throw the exception.

      ServerSocket serverSocket = new ServerSocket();
      serverSocket.setReuseAddress(true);
      serverSocket.bind(new InetSocketAddress(<port>));

------------------------------------------------------------------------------------------------------------------------

                        
If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book free here.

Tuesday, July 14, 2015

Java: Overridable method call in constructor

In NetBeans, the following code will give a warning: Overridable method call in constructor.

public class Reward {
      protected int baseReward = 1000;

      public Reward() {
            int reward = calculateReward(); //this line generates the warning
            System.out.println("The reward is " + reward);
      }

      protected int calculateReward() {return baseReward;}
}

This is because the calculateReward method can be override in subclasses and calling such a method in superclass constructor before the proper initiation of properties in subclasses may result in errors. Here is an example.

public class CreativeReward extends Reward {
      private int rewardAdjust = 100;

     public CreativeReward() {
           super();
     }

      public CreativeReward(int rewardAdjust) {
            this.rewardAdjust = rewardAdjust;
      }

      protected int calculateReward() {
          return baseReward + rewardAdjust;
      }

      public static void main(String[] args) {
            new CreativeReward(); //Prints 1000 instead of 1100
            new CreativeReward(50); //Prints 1000 instead of 1050
      }
}

This is because the constructor in superclass is called first, after which the constructor in subclass is called and the instance variables are initiated. Therefore, an overriden method called in superclass constructor will ignore the values assigned to the used instance variables in subclasses. Thus, in the above example the rewardAdjust used in the superclass constructor is 0 instead of 100 or 50.

-----------------------------------------------------------------------------------------------------------------------

                        
If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book free here.


Tuesday, July 7, 2015

SQL: How to replace values of a field in the select result with some other values?

Let say that you have a student table, which has a column called GroundExam to indicate if the status of the student on the ground exam required by the education board. The column is a numeric field. The following is the meaning of the values.

0 : have not taken
1 : Passed
2 : Failed

Now, you would like to query the Student table to see the status of the students on the ground exam, but you would like the result to display the string which is more understandable than the numbers.

You may use the following query.

SELECT NAME, ID,
          CASE  GroundExam WHEN 0 THEN 'Have not Taken'
                    WHEN 1 THEN 'Passed'
                     WHEN 2 THEN 'Failed'
                    END AS GroundExam
FROM Students;

----------------------------------------------------------------------------------------------------------------------

                        
If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book free here.