Sunday, December 5, 2010

Port versus Socket

Ive been quite confused over the terms, Socket and a Port myself.

I thought it would be better if would write it down in my blog as I've been working with them for a while.

Why a port or a socket is required ?

  1. So basically if we want to build an application that has networking features in it, we would have to run it on a port number. Different processes (application) run on different ports in order to differentiate the services offered in an application. Example: Yahoo Messenger on your system will run on a port (say 5678) and SKYPE on your system will run on a port (say 7890). If you contact port 5678, basically you are contacting Yahoo Messenger. I believe you get the idea.
  2. Multiple clients would want to connect to a server and request some data (data/service/query on a port).
  3. In order to differentiate multiple clients, we create separate pathways called "sockets" for each unique client request.
  4. Depending on the protocol (udp, tcp, etc) we make use of these to cater to the requests over a network.

Abstract Description:

Port: It is a number associated with an application running on a system. So if we want to contact a remote process running on a system/server from a client, then we would need the application (process) port number. We would also need the ip address of the system on which it is running in order to contact it.
Popular port numbers include

  • 80 (for HTTP requests on a APACHE Web server)
  • 25 (SMTP for email)

Socket: We will also need to differentiate different requests coming on a port number. In order to do that for each request we create a socket. So an application may be running on a port with multiple sockets (which are created when a request has been received on a port from a client).

So in order to request a webpage, our Web Browser contacts the port 80 on a Web Server and establishes a connection. To uniquely differentiate a request, the Web Server creates a socket for each request it receives and sends data and closes the socket.


So as this link describes http://wiki.answers.com/Q/Difference_between_port_and_socket

 SOCKET= PORT (on server) + IPADDRESS (of client)

THIS WAY A SERVER CAN DIFFERENTIATE MULTIPLE REQUESTS FROM MULTIPLE CLIENTS AT THE SAME TIME.

Tuesday, October 5, 2010

ntp update clock

Hi all.

In this post, I explain how to update the time with ntpdate command in bash shell.

I have written the shell script to test if your Linux OS is Ubuntu or a Fedora and then execute the appropriate command to install ntp and sync your clock with an ntp server.

  • If you are a fedora user, you need to first add yourself to the sudoer's list before executing this shell script.

In order to add yourself to the sudoer list, you do the following:

  • execute the command "su" in terminal to login as root, give the username and password to login.

  • After you follow the above step, execute the command "visudo" and press "i".
  • Then you browse through the terminal (by up or down key) to find the text "root    ALL=(ALL)    ALL". 
  • Then below the line you add: "    ALL=(ALL)    ALL" like in "rahul    ALL=(ALL)    ALL" in the same format as in for "root    ALL=(ALL)    ALL". 
  • Press ":" (shift semicolon) and enter "wq" and press enter key.


All you are doing is entering the visudo and adding yourself to the sudoers list and saving the file in (vi command mode).

Now your user is capable of executing sudo commands (like an install command).

Ubuntu users need not do the above mentioned steps.

Now write the following commands in a .sh file (say ntpexecute.sh) and save it.


Now run the program by typing ./ntpexecute.sh

Thursday, September 9, 2010

installing TinyOS on Ubuntu

In this post I explain how to install TinyOS on Debian systems (ubuntu 10.4+, debian).

The steps are quite simple and are explained as follows:

1) Make sure that you are connected to the internet.

2) Open the source.list file in root mode using:

3) Add the following text:
You can replace the lucid with hardy/gutsy depending on the Ubuntu version, lucid is for 10.4 Ubuntu LTS) you are using. You are doing this to make sure that your package installer finds the packages urls, etc(software sources).

4) Open a Terminal and type the following command:
You are doing this to update the package information (deb package) on your system.

5) Now we install TinyOS by executing the following command:
It will prompt you to install JRE (if not installed), go ahead and install it by following the instructions on the screen.

6) Open the .bashrc file in your home directory and add the source tinyos.sh script to the file at the end. You can also do this by giving following command:


7) Now close the Terminal and open a new one. You will a message in the terminal saying: This means that tinyOS has been successfully installed on your system.

    Friday, September 3, 2010

    HashAnvik: A MD5/SHA-1/SHA-256 hash checker for Linux

    Hi Guys!! This is my first project in wxPython and I'm very excited. I have written a wxPython application to check hash'es of binary/text/other files. This application supports hashes in MD5(most popular), SHA-1 and SHA-256 (other popular) hashes.

    It's available for download at sourceforge website at:  https://sourceforge.net/projects/hashanvik/

    Written in wxPython, it is cross-plaform (though, I'm yet to upload a Windows executable. I promise to upload it soon).

    You can checkout the source code available in the file(deb) by extracting/un-packing the deb file. It's released under the GNU GPL license, you are free to edit/reuse the source-code for your purposes.


     Happy Hashing!!

    Friday, August 13, 2010

    Reading data from WebCam on Python using OpenCV

    Hi Guys!!

    This is a simple program (I mean it!! Its a 14 line python script) to read WebCam data live using Python and I have run the program on IDLE 2.6.

    Btw, My other programs in OpenCV will be posted here.

    I'll assume that you have OpenCV 2.0/2.1 installed on your Computer System. If you haven't, you may want to refer Installing OpenCV and then run this program.

    The code:


    Friday, August 6, 2010

    Configure CDT and GTK+ Eclipse on Ubuntu

    I have found lot of people searching on the internet for the right way to install GTK and configure it with Eclipse, but found no perfect solution posted on the web (except for discussion in forums). I'm writing this down so that you guys might not suffer as i did in configuring GTK and Eclipse.

    1. First i would ask you guys to install Eclipse from the official website: http://www.eclipse.org/downloads/  . I would ask you to install from the Tarball available on the web instead of sudo apt-get install. This is my personal preference. Extract it into a folder.
    2. Install CDT from help->install new softwares or download the respective CDT file and install it on Eclipse.
    3. Install GTK development libraries from package manager. Give the following command:

      sudo apt-get install gnome-core-devel build-essential
      libgtk2.0-dev libgtk2.0-doc dev-help
        (The good thing about Step 3 is that you don't have to
      configure GTK i.e, extract, compile, install, configure
      and make  programs detect the existing library)
    4. Open Eclipse. Open a new Eclipse Project by going to new->c++ project->Executable->Hello World C++ project and select Linux GCC.
    5. Open the source file and copy paste the sample, hello world code in the editor (available at http://library.gnome.org/devel/gtk-tutorial/stable/c39.html) and save it.
    6. Go to the project properties->C/C++ build->settings-> GCC C++ Compiler and in Command line pattern paste the following: ${COMMAND} `pkg-config --cflags --libs gtk+-2.0` ${FLAGS} ${OUTPUT_FLAG}${OUTPUT_PREFIX}${OUTPUT} ${INPUTS}   all we are doing is adding `pkg-config --cflags --libs gtk+-2.0` to the existing command line pattern. We are adding this line so that while compiling it would find the libraries/headers.
    7. Now, do the above for GCC C++ Linker in properties->C/C++ build->settings-> GCC C++ Linker.
    8. Click on OK, go to Project->build project and it will build it successfully. Now, you can run your project.
    Basically we are telling the compiler to add `pkg-config --cflags --libs gtk+-2.0` while compiling.

    For compilation in Terminal, we can use the following:
    g++ `pkg-config --cflags gtk+-2.0`  `pkg-config --libs gtk+-2.0` test.cpp -o test

    Happy C++ programming on Eclipse.

    Cheers,
    Rahul Kavi.

    Wednesday, July 7, 2010

    Linux-Gnome Open Terminal in current folder

    One of the most useful shortcuts in Gnome-Linux is "open folder here".

    Bing/Google searches have given me a result asking me copy a shell script which opens a terminal in the present folder. This was a tweak.

    I am happy that, there is a easy to use deb available on the web that does this for us.

    Open the terminal and give the following command:


    Now restart Nautilus:
    Writing it down so that people out there may find it useful and also for my future reference.

    Tuesday, May 18, 2010

    Using OpenCV 2.3 on Ubuntu 10.04

    All versions of OpenCV can be installed in the follownig manner from the sourcecod (tar ball) on Linux.

    My other programs in OpenCV will be posted here

    OpenCV on Ubuntu (also with Python and FFMPEG support build enabled):
    http://developerstation.blogspot.com/2011/03/opencv-in-python.html

    You can configure your Eclipse to develop OpenCV programs by giving the right parameters to the GCC compiler in Ubuntu. Click here for to see how to configure CDT (c-development environment) in Eclipse on Ubuntu.

    Writing it down, because it can serve as a future reference.

    Thursday, May 13, 2010

    Introduction to Computer Vision

    For all those trying to understand the basics of Computer Vision, this is the best place to start:

    http://vision.stanford.edu/~birch/projective/

    It covers basic math required to understand the computer vision,

    Thursday, April 29, 2010

    implements Runnable vs extends Thread:Java

    In order to implement Multi-Threading in Java, one must implement the Runnable interface OR extend Thread class. I have read in many forums people asking which one to use, does one of the above ways have any advantages over the other, similar questions.

    I also sometimes pondered on this. I just thought for a while, i realized that one cannot extend more than one class in Java (unlike C++).

    So, if one has to write a class that does multi threading, one has to prefer doing the same using the implementing the Runnable interface as one is not really worried about "extending" features of Thread class, rather we are interested in using multi-threading features.

    It also adds up that, implementing an interface gives us an option of extending a class. But if, we extend a class(Thread) we do not have an option of extending other class (if required).


    Here is a good article that i found on Google:
    http://stackoverflow.com/questions/541487/java-implements-runnable-vs-extends-thread

    Friday, April 23, 2010

    Generate Hash of a file: Java

    I google'ed/bing'ed a lot to find articles on generating a hash of a file and found none. So, i decided to write one myself.

    In this article i explain about generating a hash for a file in Java.

    We make use of "java.security.*" API classes to achieve the task.


    In the following program i am generating the hash of the java source of this program (it need not be so, you can generate hash for any file).




    The method

    getInstance in 

    MessageDigest class takes name of the hashing technique/Algorithm as a paramter

    refer Java API at



    http://java.sun.com/j2se/1.4.2/docs/api/java/security/MessageDigest.html






    Friday, March 19, 2010

    Orkut "Aw, Snap!" problem

    For all the users that have been experiencing the "Aw, Snap!" problem on Google Chrome (4.1 or previous versions) while accessing Orkut is due to the AdBlock plus extension add on installed.

    It also might be due to combination of ill effects of one or more add ons. Just disable one or more add ons in a random way and you will see the problem solved.

    Im not sure if google will solve the problem soon. It should be embarrassing for them
    as a user is trying to access a google service from a google browser and one gets an error message.

    I thought it might be useful to share with all you.

    Monday, February 22, 2010

    time taken to execute a Java Program

    Hi Guys.......In this article i would describe to write a program in Java to calculate its execution time.

    Its simple as follows:
    • Calculate time at beginning of the program execution.
    • Calculate time at the ending of the program.
    • Calculate the difference and display.
    To calculate the time in millisecs, we use the method System.currentTimeMillis();

    The above program looks perfectly fine and makes sense too!!! But there is a concept of static block in Java, where code inside a static block executes first.
    Now you would say first line of code to be executed in java would be the one in main method. Why dont you try the following and see it for urself.

    Output: this is from static block this is from main method:

    Now, you might think the following code looks funny, but it does run.


    Output: this is from static block this is from static block too!!

    this is from main method:


    Now this totally out of topic!!

    But wait, this does make sense in using this concept in our program.
    If you guessed that the starting time in millisec should be calculated in static block, then you are right...

    Its simple as follows:

    • Calculate time at beginning of the program execution in static block.
    • Calculate time at the ending of the program (now you can say that the we can write this line in garbage collector block, but im not sure if we can override the garbage collector method. Even if we did find a way to do it, we cannot be sure if the code would be executed when we want it to be. Its controlled by virtual machine but when we want it to execute) .
    • Calculate the difference and display.



    I agree that this does not achieve a significant precision, but this is the way to do it. Any comments are welcome if there is a better way other than this one.

    Wednesday, January 20, 2010

    Implementing a Queue: Data Structures in Programming

    Implementing a Stack is a simpler task compared to a Queue. In this post, i will explain how to implement a Queue. One can imagine a queue as a Water Pipe where water (elements, integers/objects in terms of computer programmer like us) enters from one end and leaves the other end. It follows the concept of "First In First Out"........

    Implementing a queue through a program in python is quite easy, and is quite similar (only in python) and is described as follows:



    Now, we see that it is so simple to implement in python. Now we will do the same in JAVA. There is a way through which we can implement the same as above in JAVA too (by importing java.util.Queue)!!

    But what fun is it, when we let JAVA take care of everything (like overflow/empty.... kind of situations).

    I will implement a small Queue of Integers and insert integers.

    The size of the queue is read dynamically at the console (command prompt or the Bash shell or whatever).




    The above code can be implemented in a more precise, professional manner using the Queue class.

    Why re-invent the wheel when the stuff is pre-coded in the API?

    Its because, we need to know what is going on behind the scenes, what the logic is all about. Thinking from scratch helps us understand and see stuff. This helps us professionally.


    Cheers!!!
    Rahul Kavi.

    Tuesday, January 19, 2010

    Implementing a Stack: Data Structures in Programming

    I remember implementing a stack during one of programming assignments during my undergrad years. I have known that many of us have a phobia of implementing datastructures (especially using C). I believe that if we were taught implementing datastructures using simpler languages such as Python and Java (i.e. without use of pointers), we would been better off by now. Though i do not blame C language, it all requires some time to be spent with the language to understand it and implement the logic.

    In this post, i am writing how to implement a stack using a simple language like Python.

    Stack is a simple data structure which takes data elements at one end and pops the data elements at the same end.

    Refer this wikipedia article for more clear explanation.
    http://en.wikipedia.org/wiki/Stack_%28data_structure%29

    Steps in implementing a Stack:
    1. Initialize the Stack.
    2. Insert the elements.
    3. Pop the element.
    4. Follow steps 2 and 3 interchangeably as per the requirement (making sure that we are not inserting/popping the stack beyond its capacity.
    Code:

    If you can see, append method adds the element to the end of the list (similar to a Stack). Pop method returns the element at the end of the list and removes the same element from the list.

    Note:
    • In python, the size of list is not fixed. So, there would be no overflow of data from the stack (as we are implementing the stack using the list).
    • But, the size of the list can be minimum at zero(not less than it, doesnt make sense if less than zero) and is quite obvious.
    • You do not have to take care of writing the logic to insert or delete the elements in the list, python takes care of it.
    Now, lets try implementing the same in Java without making use of builting libraries or API. This helps us in understanding the same at the basic level.

    Here, the difference in implementation is that the size of the array is fixed. We are using Array to implement the Stack (in simplest form). Though we can use the Stack class @ java.lang.Stack package.

    Note:
    • While popping, we are just modifying the Top Of Stack element and reducing the index.
    • Also while popping, we are checking if the Stack is empty or not, if the stack is empty, we just display an message that we cannot pop further.
    • While inserting, we are making sure that index is within the range of maximum number of elements in the stack. This ensures, that we do not raise an Index Out of Bounds error.
    • We seperated the tasks into methods implemented in Stackx class.


    Most of the code is self explanatory:




    Output:

    Contents of Array after pushing:1 at:1:

    1--->
    Contents of Array after pushing:2 at:2

    1---2--->
    Contents of Array after pushing:3 at:3

    1---2---3--->
    Contents of Array after popping: 3

    1---2--->
    Contents of Array after popping: 2

    1--->
    Contents of Array after popping: 1

    >
    you cannot pop

    Array is empty

    Contents of Array after pushing:1 at:1:

    1--->
    Contents of Array after pushing:2 at:2

    1---2--->
    Contents of Array after popping: 2

    1--->


    The output is self explanatory too!!