Monday, December 31, 2012

Which algorithm is efficient for swapping?

Swapping of two numbers/characters can be done by different ways. Generally all people know swapping using a temporary variable. 
In this post i will tell you how to swap two variables in 3 different ways,  and which algorithm is efficient to swap the variables. 

Here all the below examples are shown in C code(executed in linux environment with gcc compiler).

Method 1:
  • Swapping using a temporary variable:

Friday, December 7, 2012

How to write a UDP socket program in Qt


We have seen how to write a simple UDP socket program in C (Linux) in the earlier post..

Now we are going to learn how to write a simple UDP socket program in Qt..

In the following example we used to send and receive data on localhost address.
And the readyRead() signal and slot is used whenever the data receives on the given socket (i.e. socket reday for read data), the control goes to slot method.
writeDatagram() and readDatagram() API's used for sending and receiving data on the given udp socket.

Before starting the Program to write, create a Project using following steps 
  • open Qt creator
  • Create Project
  • Select Other Project in Projects tab then select Qt Console Application, then click on choose...
  • Then give the Project name
  • And then click on Next, Finish in the following dialog boxes
  • Then open the .pro file and write QT += network 
  • To create the cpp file Right click on the project then choose Add new

Monday, November 26, 2012

Linux Shell scripting basics and Simple example to add two numbers

Shell scripting is a file which contains the list of commands for executing. What is Shell? Shell is special program or application provided for the user interaction.
There are different kinds of shells in Unix/Linux, they are
  • Sh: Bourne Shell
  • Csh: C Shell
  • Ksh: Korn Shell
  • Bash: Bourne again Shell.. etc
Some of the advantages of the shell scripting are,
  • Automation of tasks such as backups,log monitoring etc
  • To do repetition of of tasks
  • Save lots of time..
Unix/Linux Shell scripting also containg the functions, control statements,loops like other programming languages C, C++...

Monday, November 12, 2012

Signal handling in Linux using signal() call

signal() function in linux is used to sets of handler for the given signal number, the handler is any of SIG_DFL(default action) or SIG_IGN(ignore signal) or address of user defined function which we called signal handler

syntax:
         sighandler_t signal(int signum, sighandler_t handler);

the second parameter is the address of the signal handler function (function pointer), which has the following syntax

         typedef void (*sighandler_t)(int );

Here the parameter int represents the Signal number.

Here is the simple C program which describes the SIGINT Signal handling in linux

signal.c

#include<stdio.h>
#include<signal.h>

Thursday, November 8, 2012

gethostbyname example in C


gethostbyname() function will return the host details such as hostname, aliase names of the host,type of address, and length of host address of the given hostname.

This function will return the hostent (host entry) structure which contain the all the above details. h_addr_list[0]  field of “hostent” structure will contain struct in_addr equivalent of the host address.


Sysntax

struct hostent *gethostbyname(const char *name);

Here name is any valid hostname  (or) IPV4 address in standard dot notation (or) IPV6  address in colon or dot notation


Here is the simple C Program in Linux that illustrates the gethostbyname() function.

Saturday, November 3, 2012

UDP Client Server C Program

Hii all, in this Post you will learn how to write a simple UDP Socket Program in C in linux to send a message to the server and server will reply you.
Unlike TCP, UDP does not have connection establishment.So there is no connet() system call in client and accept() systemcall in server.

So the System calls in the server are

1.socket()
2.bind()
3.recvfrom()
4.sendto(
and I client the system calls are
1.socket()
2.sendto()
3.recvfrom()

The simple UDP Client-Server Program is as follows


udp_server.c

Friday, October 19, 2012

C program for getting Linux system information


In most of the Linux systems we use the command “uname” to print the system information in the terminal.
Ex:
           $uname -a

which gives the information about the system such as OS name, kernel version, release,domain name etc... Similerly we can get that information through C Programming.

Now I will let you how to print Linux system information through C programming in Linux, by using the structure “struct utsname”, and the system call uname() which return 0 on success, -1 on error.. See the code snnippet given below...

uname.c:

#include<stdio.h>
#include<sys/utsname.h>

Thursday, October 4, 2012

QGraphicsView Example Program by inheriting QGraphicsView class


In the prevoius qgraphicsview example we created the view (QgraphicsView) and placed the scene in this view.Now in this example we created a class and inherit this class from QGraphicsView class, so the class being inherited acts like view and we will set the scene for this inherited class.

Check the example program and the output below...
  • graphics.h

#ifndef GRAPHICS_H
#define GRAPHICS_H
#include <QtGui/QWidget>
#include <QGraphicsEllipseItem>
#include <QGraphicsScene>
#include <QGraphicsView>
class graphics : public QGraphicsView
{
    Q_OBJECT
    QGraphicsScene *scene;
    QGraphicsEllipseItem *itm_ellipse;
    QGraphicsTextItem *itm_txt;
    QGraphicsLineItem *itm_line;
    QGraphicsPolygonItem *itm_poly;
    
public:
    graphics(QWidget *parent = 0);
    ~graphics();
};
#endif // GRAPHICS_H

Monday, October 1, 2012

QgraphicsView simple example program


Here is a simple QgraphicsView example program in Qt.

In this example we created a scene (QGraphicsScene) and placed all the items  in this scene by specifying the scene object while creating the graphics item.Andat last we created the view (QGraphicsView) and scene is placed in this view by using setScene() method of QgraphicsView.



#include <QtGui/QApplication>
#include<QGraphicsView>
#include<QGraphicsScene>
#include<QGraphicsSimpleTextItem>
#include<QGraphicsRectItem>
#include<QGraphicsEllipseItem>
#include<QVector>
#include<QGraphicsPolygonItem>

int main( int argc, char **argv )
{
    QApplication app( argc, argv );
    QGraphicsScene scene( QRect( -100, -100, 400, 400 ) );
    scene.setBackgroundBrush(Qt::cyan);


    QGraphicsSimpleTextItem *textItem = new QGraphicsSimpleTextItem(
                "technoyouth10", 0, &scene );
    textItem->setPos( -50, -50 );
    textItem->setFlag(QGraphicsItem::ItemIsMovable);


    QGraphicsRectItem *rectItem = new QGraphicsRectItem(
                QRect( 0, -0, 200, 40 ), 0, &scene );
    rectItem->setPen( QPen( Qt::red, 3, Qt::DashDotLine ) );
    rectItem->setBrush( Qt::green );


    QGraphicsEllipseItem *ellipseItem = new QGraphicsEllipseItem(
                QRect( 80, -80, 100, 75 ),
                0, &scene );
    ellipseItem->setPen( QPen(Qt::yellow) );
    ellipseItem->setBrush( Qt::magenta );


    QVector<QPointF> points;
    points << QPointF( 20, 50 ) << QPointF( 80, 50 ) << QPointF( 110, 80 )
           << QPointF( 50, 130 )<< QPointF( -10, 80 ) ;
    QGraphicsPolygonItem *polygonItem = new QGraphicsPolygonItem(
                QPolygonF( points ), 0, &scene );
    polygonItem->setPen( QPen(Qt::red) );
    polygonItem->setBrush( Qt::yellow );

    QGraphicsView view;
    view.setScene( &scene );
    view.show();
    return app.exec();

}



Output:
Run the program by pressing Ctrl+r...


Saturday, August 18, 2012

TCP Daytime client Program using C in linux

This is the client program to get the time and date. Daytime server is the server program which is provided by the linux, and it has a specific name in the system i.e. "daytime".


daytimecli.c
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<netdb.h>

int main(int argc,char *argv[])
{
    int sockfd,len,ret;
    struct sockaddr_in saddr;
    char *hname,buff[256];
    struct hostent *hostinfo;
    struct servent *servinfo;

Thursday, August 2, 2012

Simple Client-Server network Program using TCP/IP stream sockets


This is a simple socket program, in which client sends a message to the server and server replies to the client. The server is waiting for the client process connections.

Server Program:
                                              server.c
#include<sys/types.h>
#include<sys/socket.h>
#include<stdio.h>
#include<netinet/in.h> // for networkbyte order (htons(),htonl())
#include<arpa/inet.h>
#include<string.h>
#include<unistd.h>


int main()
{
    struct sockaddr_in saddr,caddr;
    int len,listenfd,sessfd,ret,retb;
    char buff[10]={0};


Friday, July 20, 2012

What is the use memset(), memcpy(), memmove() in C language?and memset(), memcpy(), memmove() example programs in c

memset()
Syntax:
#include <string.h>
void *memset( void *str, int ch, size_t n );

Description:
The function memset() copies ch into the first 'n' characters of str, and returns str. memset() is useful for intializing a section of memory to some value. For example:
memset(array, '\0', sizeof(array) );
it is a very efficient way to set all values of array to NULL.