Sunday, October 27, 2013

Set Apache Document Root (Windows)


I always forget how to set virtual host or more than one document root in Apache server, this is just to remind myself about the changes in httpd.conf file.

Usually the document root is set as default to the www directory of your Apache installation, which Apache keeps listening on port 80. If you create your projects just within the www directory, you can pretty much access it with http://localhost/mypage.php (considering your page is called mypage.php). This looks more like working with a real website where http://localhost/ will be replaced by your registered domain name or ip address. However, if you make sub-directories inside your www directory, you will have to access them as http://localhost/mydirectory/mypage.php, but we don't want to write the specific directories separately from the base domain name, so one way to get around it is to tell your Apache to make 'mydirectory' another document root, so that you can access the pages in a 'more realistic' manner.

Now to keep things clean, we will assign new port numbers to each virtual document root other than the www directory which is listened to port 80. So, if we assign port 8080 to our new document root 'mydirectory', we can now access mypage.php using the url http://localhost:8080/mypage.php which is much better than the previous one and it will cost you less setup hassles when you deploy the project in an actual server.

Here what you need to add to your Apache's httpd.conf file:

Listen 8080

NameVirtualHost *:8080

<VirtualHost *:8080>
ServerName localhost
DocumentRoot "D:/wamp/www/mydirectory" #example path yo your new document root directory
</VirtualHost>

Don't forget to restart your Apache server.


Friday, September 20, 2013

Java: Get Instance of Inner Class


Today my good unpredictable friend Mishuk gave me a piece of Java code which is full of nested classes. I was not surprised at all as I already know his love for inner classes. Then he asked me whether it is possible to call a method from the innermost class, i.e. is it possible to get an instance of the nested classes. At first I thought how hard could it be, but it eventually turned out to be thought provoking. It was not as straight forward for me as I thought (I won't deny, I know a very little Java, which is why this little bit of trick had surprised me). So I just wanted to put it up here for future references.

The class he gave me follows:

class A {
    String name="A";
   
    public class B {
        String name="B";
       
        public class C {
            String name= "C";
           
            public void print(String name){
                System.out.println(name);
                System.out.println(C.this.name);
                System.out.println(B.this.name);
                System.out.println(A.this.name);                               
            }
        }
    }
}

As there was a restriction that the above code can't be modified, the only way to do this is to somehow get instances in a hierarchical manner. Because you cannot get instances of B without creating an instance of A and so on. But Java has a cool thing called reflection, and using reflection, it is possible to get instances from the inner class as well. So here is how the print() method from class C can be called. The idea is to create instances following the hierarchical order and then use the last one. Now it seems easy!

import java.lang.reflect.*;

class Main {
    public static void main(String[] args) throws Exception {
        Class classAB = A.B.class;
        Class classABC = A.B.C.class;
        Constructor constructorAB = classAB.getConstructor(A.class);
        Constructor constructorABC = classABC.getConstructor(A.B.class);
        A instanceA = new A();
        A.B instanceAB = constructorAB.newInstance(instanceA);
        A.B.C instanceABC = constructorABC.newInstance(instanceAB);
        instanceABC.print("JAVA SUCKS WITHOUT REFLECTION");
    }
}

Oh, one more thing, for those who don't know this yet and too lazy to run some Java codes, here is the output:

JAVA SUCKS WITHOUT REFLECTION
C
B
A

Yah I know this may be done in hundreds of different ways and exceptions should be handled and and and and .... who cares?


Wednesday, July 3, 2013

Simple Chat Program in C (Part 3)


Read Part 2 : The Client

Sample Execution:

$ gcc server.c -o server -lpthread
$ gcc client.c -o client -lpthread

Now we run the server program and several client programs

Each client should first connect to the server by typing in:

login [alias]
Here [alias] is optional, if it is not mentioned, the name will be set as Anonymous.

The name can be changed by the command:

alias [new_name]
After this, you alias will be changed to the new name you have given.

To send a message to everyone connected at the moment:

send [message]

To send a message to a specific alias:

whisp [user_alias] [message]
If the target alias is not active at that moment, message will not be sent anywhere

You can disconnect from the server by typing:

logout
You can again reconnect by using the login command

You can terminate the client by typing:

exit

This is pretty much of it. Basically this is good for your C lab assignments regarding TCP / socket based chat clients. Here is a screen shot from my computer:

Good luck!