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};