Sunday, November 18, 2007

TechBlog: GTK+ programming starters

TechBlog: GTK+ programming starters

GTK+ programming starters

Hey friends,

Bored of programming in CUI (character user interface), wanna do some GUI programming, want it to run in Linux as well as in the Windows environment. then GTK+ is for you.


GTK stands for GIMP Tool Kit.

GTK is a a multi-platform library for creating graphical user interfaces. It is designed to be small and efficient, but still flexible enough to allow the programmer freedom in the interfaces created. GTK allows the programmer to use a variety of standard user interface widgets such as push, radio and check buttons, menus, lists and frames. It also provides several "container" widgets which can be used to control the layout of the user interface elements.


To make life easier for you, GTK presents this flexibility in a uniform framework. Specifically, it implements its own support for object oriented programming that is well adapted to the purposes of a user interface toolkit and it aims at providing a reasonable sane and disciplined programming interface. This uniformity and discipline is intended to make it easy and reliable to access GTK from languages other than C


Ok , enough of the talk... lets start on some hands on coding for a better understanding of the library.



I would suggest you to download and install the Glade before starting.

Glade is a RAD tool to enable quick & easy development of user interfaces for the GTK+ toolkit and the GNOME desktop environment.

It is very similar to the Visual Basic development environment. With all drap and drop features you can come up with a proffessional looking software within minutes.

I would tell you how to make an application that would have 2 buttons - one to eject the CD tray, and the other to Load the CD tray.

I will use the Linux environment ( but it is entirely my choice, you may use Windows in case you want to)


OK. Here it comes ...





Step 1. Open Glade and Start a new project.





Choose Project -> New.





Step 2. Choose 'Window' from the palette. This is the main window, and we would build everything on it.





Add 'Vertical Boxes' and make 2 rows.





Add 2 buttons by choosing 'Buttons' from the Palette toolbar and add one to each row.


Step 3. Now Click on the Button1 and choose View -> Show Property Editor.





Put a proper name for the button1 as "Eject" and choose "Load" for button2.





Choosing button1 only, go to the Signals tab and choose 'clicked' in the signal. Click on Add button. This is the callback function for the signal. That is whenever there is an event of mouse click on the button1, the on_button1_clicked() function will be invoked.





Do the same with button2.


Step 4. Save the project and note the path it is saved.






The project should look a bit like this now !




Step 5. 'Build' the project. This creates all the .c files required for the project and automatically puts in barebone code in it too!!





Step 6. Here comes the hard core coding part, but do not fear !


In the Projects directory, go to the src folder. This is where the actually codes are saved. There should be a number of files in here, but the one of our concern is the callbacks.c file.


open it. you should see two empty functions -- void on_button1_clicked and void on_button2_clicked.





Now, add this code to the on_button1_clicked: system("eject");

and in the on_button2_clicked add: system("eject -t");





eject and eject -t are the shell commands to eject and load a CD tray respectively. We are passing this to system() function. The system function takes a string arguments and runs it in a shell. So what we effectively get is a CD tray opener !





Now save and close the file.





Step 7. In the parent directory, run the autogen.sh script from the terminal, by typing ./autogen after going to the parent directory. This should compile and build the project and create an executable file. Run the executable file and enjoy !!!








You have built a professional level GUI program. If you compile/ build it in windos environment, you would get an windows executable.



Simple, isn't it?




In case you have doubts or you want to know more, write comments here. I am also available at my email address given.

Friday, July 6, 2007

The internet

Hey folks!
Good to be back after a long time.

The topic I want to tell you about today is "internet".
well let me clear this first. The word "internet" has been created from - "interconnection of networks".

Any interconnection of networks is termed as an internet.
There is a slight difference between "internet" and THE "Internet" ( notice the capital I). The Internet is the World Wide Web (WWW) as we know of. This is also a interconnection of networks, but this consists of interconnection of a number of networks spread throughout the world.

The internet I am talking about is any general interconnection of networks.
The internet is logically divided into a number of layers, with the layers arranged in a stack like fashion. The layers are namely -

(from the top to the bottom)
1. Application layer
2. Transport layer.
3. Datalink layer.
4. The host to network layer.

The tasks and roles of each layers are well defined and so is the interface between any two layers are well defined. This make is easier to change or modify any layer without disturbing of effecting any other layer.

The Host to Network layer is the layer that is made up of the electrical signals that pass through the wires and make up the 1s and the 0s.

In the data link layer, the information moves in the form of 'packets' - which are units of data. The have a header as well as a trailer attached to it.

The transport layer is the layer that takes up teh responsibility of setting up a connection and ensuring that the data arrives in order and is dutyfully acknowledged (in case of connection oriented protocols) and is ultimately brought down gracefully.

All the network based programs reside in the application layer.


Will talk more about the layers in details later on. Till then, enjoy !!

Monday, December 25, 2006

Inetd

Recently while dabbling over socket programming, I came across a nifty feature known as inetd. It is basically a daemon that "listens" to the ports of the some well known servers and starts the server when it detects some request coming at that port.

For the uninitiated, a class of programs that work on the network are classified as "server-client" paradigm. The basic idea behind it is there would be a remote server program which would serve any request a client may send at it. For this kind of communication, a form of address is required. The address in this case is specified in the form of IP address and port number. Each Server has a port number and an IP address of the computer it is running on. The port number is pre determined and known by the client. The Server listens on the specified port for incoming requests. Whenever a client program sends any request to the server program (via its IP address, portnumber), the server processes the request and sends back the data to the client with the address specified by IP addres, portnumber.

Nowadays, a computer may run a number of servers, from http servers, mail servers and ftp servers. These servers need to running since the booting of the computer, listening for requests
Now the server uses up computer resources even when it is not performming any thing useful work instead just listening on its alloted port for incoming request.

The inetd deamon was built to address this situation. What the inetd does is instead of starting all the servers at boot time, only the inetd daemon is started. The inetd listens to all the ports that there are servers. It uses a API called SELECT, which allows a program to listen to a number of sockets simultaneously. The inetd runs in a loop and waits until the SELECT call returns. Whenever it detects an incoming connection, the SELECT call returns with the port number on which the incoming connection came. The inetd then understands which server to start based on the port number on which the request arrived.

What the inetd does is it "forks" and create a child process. The child process then exec the server process. Then the server process starts running. The server process uses the API getpeername() to get the address of the client process that is calling it. The inetd redirects the port of the incoming request to act as the standard input and standard output of the server process. It is to be noted that the file descriptors survive even across a exec call.

The server runs as an independent process, and when the server finishes serving the client, it ends and returns to the inetd process. The inetd again starts listenning to that port again and repeats the process for any incoming request.

Each server that runs are runned in the form of a deamon. So that it is detached from any terminal and runs independently. The process of making an running program into a daemon involves two forks and some clearn-up work. The process remains the same for all programs. Before inetd was introduced, all the servers had to independently make itself in to a daemon and then run. But with inetd, it is not required. Since inetd makes itself a daemon, all it child processes are not attached to any terminal, hence they are daemons automatically. So the programmer needs not worry about making there porgram into a daemon. The servers can now read and write as if it is reading and writing for the standard input and output. This makes the task of coding the servers eaiser.



This is just an introduction of inetd. This is intended for beginners. Please Write your comments telling me how you feel about this peice and how you want it to be improved. I would really appreciate you taking time out and writing your views

Thanks