Loading

Archive for 11/01/2012 - 12/01/2012

Client - Server Program in C - Socket Programming

Server File - 


#include<unistd.h>
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<string.h>
#include<stdlib.h>
#include<pthread.h>

#define MAX 30

int AddDigits(int number);
int Factorial(int number);
int StringLength(char str[]);
int StringCompare(char str[],char str1[]);
char* StrConcat(char str1[],char str2[]);
char* SubsString(char str1[],int start,int end);

int ssfd;
int ccfd,res;
int serverlen,clientlen;
struct sockaddr_in serveraddr;
struct sockaddr_in clientaddr;
char result[MAX];

typedef struct message
{
char msg[MAX][MAX];
char msgoutput[MAX];
int input;
int ch;
int output;
}message;


void main()
{

int msgLen;
char *ch;
ssfd=socket(AF_INET,SOCK_STREAM,0);
message *msg;
message *reply;

msg=(message*)malloc(sizeof(message));
reply=(message*)malloc(sizeof(message));

printf("\nSSFD Value:%d",ssfd);

msgLen=sizeof(message);


serveraddr.sin_family=AF_INET;
serveraddr.sin_addr.s_addr=inet_addr("127.0.0.1");

//serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);
//serveraddr.sin_port = htons(9734);

serveraddr.sin_port=9734;
serverlen=sizeof(serveraddr);
bind(ssfd,(struct sockaddr*)&serveraddr,serverlen);
printf("\nConnected to socket");
listen(ssfd,5);

ccfd=accept(ssfd,(struct sockaddr*)&clientaddr,&clientlen);
printf("\nConnection accepted successfully");
while(1)
{
read(ccfd,reply,msgLen);
switch(reply->ch)
{
case 1:
reply->output=AddDigits((int)reply->input);
write(ccfd,reply,msgLen);
break;
case 2:
reply->output=Factorial((int)reply->input);
write(ccfd,reply,msgLen);

break;
case 3:
reply->output=StringLength(reply->msg[0]);
write(ccfd,reply,msgLen);

break;
case 4:
reply->output=StringCompare(reply->msg[0],reply->msg[1]);
write(ccfd,reply,msgLen);
break;

case 5:
strcpy(reply->msgoutput,StrConcat(reply->msg[0],reply->msg[1]));
write(ccfd,reply,msgLen);
break;
case 6:
reply->output=Palindrome(reply->msg[0]);
write(ccfd,reply,msgLen);
break;

case 7:
strcpy(reply->msgoutput,SubsString(reply->msg[0],reply->input,reply->output));
write(ccfd,reply,msgLen);
break;
case 8:
system("xeyes");
break;
}

}

}
int AddDigits(int number)
{
int result=0;
while(number/10!=0)
{

result+=number%10;
number=number/10;
}
result+=number%10;
return result;
}

int Factorial(int number){
int result=1,i;
for(i=1;i<=number;i++){
result*=i;
}
return result;
}

int StringLength(char str[]){
int result=0;
while(str[result]!='\0'){
result++;
}
return result;
}


//0    ===> Strings are equal....
//1    ===> Strings are not equal....
int StringCompare(char str[],char str1[])
{
int result;
int i,j;
for(i=0;i<StringLength(str);i++){
if(str[i]!=str1[i])return 1;
}
return 0;
}

//0    ===> Palindrome...
//1    ===> Not Palindrome...
int Palindrome(char str[])
{
int result;
int i,j;
for(i=0,j=strlen(str)-1;i<StringLength(str);j--,i++){
if(str[i]!=str[j])return 1;
}
return 0;
}

char* SubsString(char str1[],int start,int end){
int i,j=0;
//strcpy(result,str1);
for(i=start;i<=end;i++){
result[j++]=str1[i];
}
//result[j]='\0';
return (char*)result;

}

char* StrConcat(char str1[],char str2[])
{
int i=0,j=0;
strcpy(result,str1);
strcat(result,str2);
//printf("Result %s",result);
return (char*)result;
}


Client File -




#include<unistd.h>
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<string.h>
#include<stdlib.h>

#define MAX 30

typedef struct message
{
char msg[MAX][MAX];
char msgoutput[MAX];
sint input;
int ch;
int output;
}message;


void main()
{
int ssfd;
int len;
int msgLen;
struct sockaddr_in address;
int result;
FILE *fp;

char ch='A';

message *msg;
message *reply;

msg=(message*)malloc(sizeof(message));
reply=(message*)malloc(sizeof(message));
msgLen=sizeof(message);
//strcpy(msg->msg,"Aniket");

ssfd=socket(AF_INET,SOCK_STREAM,0);

address.sin_family=AF_INET;
address.sin_addr.s_addr=inet_addr("127.0.0.1");
address.sin_port=9734;
len=sizeof(address);

result=connect(ssfd,(struct sockaddr*)&address,len);
//fp=fdopen(ssfd,"rw");
if(result==-1){
perror("Server not found");
printf("\nError Result:%d",result);
//exit(1);
}

printf("\nConnection successful");

do{
printf("\n\nSELECT Appropriate Option:\n");
printf("\n1.\tAddition of Digits");
printf("\n2.\tFactorial of a Number");
printf("\n3.\tLength of string");
printf("\n4.\tString Comparison");
printf("\n5.\tString Concatenation");
printf("\n6.\tCheck For Palindrome");
printf("\n7.\tFind Substring.");
printf("\n8.\tRun Command");

printf("\n9.\tDisconnect And Exit");
scanf("%d",&msg->ch);
printf("Your Choice:%d",msg->ch);
//write(ssfd,msg,msgLen);
switch(msg->ch){
case 1:
printf("\nEnter Number:");
scanf("%d",&msg->input);
write(ssfd,msg,msgLen);
read(ssfd,reply,msgLen);
printf("Output from server:%d",reply->output);
break;
case 2:
printf("\nEnter Number:");
scanf("%d",&msg->input);
write(ssfd,msg,msgLen);
read(ssfd,reply,msgLen);
printf("Output from server:%d",reply->output);

break;

case 3:
printf("\nEnter any string:");
scanf("%s",msg->msg[0]);
write(ssfd,msg,msgLen);
read(ssfd,reply,msgLen);
printf("Output from server:%d",reply->output);
break;

case 4:
printf("\nEnter any First String:");
scanf("%s",msg->msg[0]);
printf("\nEnter any Second String:");
scanf("%s",msg->msg[1]);
write(ssfd,msg,msgLen);
read(ssfd,reply,msgLen);
printf("Output from server:%d",reply->output);
if(reply->output==0)
printf("\nStrings are equal...");
else
printf("\nStrings are not equal...");

break;

case 5:
printf("\nEnter any First String:");
scanf("%s",msg->msg[0]);
printf("\nEnter any Second String:");
scanf("%s",msg->msg[1]);
write(ssfd,msg,msgLen);
read(ssfd,reply,msgLen);
printf("Output from server:%s",reply->msgoutput);


break;

case 6:
printf("\nEnter any string:");
scanf("%s",msg->msg[0]);
write(ssfd,msg,msgLen);
read(ssfd,reply,msgLen);
printf("Output from server:%d",reply->output);
if(reply->output==0)
printf("\nString is a palindrome");
else
printf("\nStrings is not palindrome");

break;
case 7:
printf("\nEnter any string:");
scanf("%s",msg->msg[0]);
printf("\nEnter Start Index:");
scanf("%d",&msg->input);
printf("\nEnter End Index:");
scanf("%d",&msg->output);
write(ssfd,msg,msgLen);
read(ssfd,reply,msgLen);
printf("Output from server:%s",reply->msgoutput);
break;
case 8:
printf("\nEnter String-\"xeyes\" or other valid command:");
scanf("%s",msg->msg[0]);

write(ssfd,msg,msgLen);
system(msg->msg[0]);
read(ssfd,reply,msgLen);
break;
default:
printf("Invalid Choice....Select Another");
break;
}
}while(msg->ch!=9);

close(ssfd);
//exit(0);
}



/*
OUTPUT

desktop ~ $ gcc server.c
desktop ~ $ ./a.out

SSFD Value:3
Connected to socket


desktop ~ $ gcc -o client.out client.c
desktop ~ $ ./client.out

Connection successful

SELECT Appropriate Option:

1.    Addition of Digits
2.    Factorial of a Number
3.    Length of string
4.    String Comparison
5.    String Concatenation
6.    Check For Palindrome
7.    Find Substring.
8.    Run Command
9.    Disconnect And Exit1
Your Choice:1
Enter Number:1234
Output from server:10

SELECT Appropriate Option:

1.    Addition of Digits
2.    Factorial of a Number
3.    Length of string
4.    String Comparison
5.    String Concatenation
6.    Check For Palindrome
7.    Find Substring.
8.    Run Command
9.    Disconnect And Exit2
Your Choice:2
Enter Number:5
Output from server:120

SELECT Appropriate Option:

1.    Addition of Digits
2.    Factorial of a Number
3.    Length of string
4.    String Comparison
5.    String Concatenation
6.    Check For Palindrome
7.    Find Substring.
8.    Run Command
9.    Disconnect And Exit3
Your Choice:3
Enter any string:abc
Output from server:3

SELECT Appropriate Option:

1.    Addition of Digits
2.    Factorial of a Number
3.    Length of string
4.    String Comparison
5.    String Concatenation
6.    Check For Palindrome
7.    Find Substring.
8.    Run Command
9.    Disconnect And Exit4
Your Choice:4
Enter any First String:xyz  

Enter any Second String:xyx
Output from server:1
Strings are not equal...

SELECT Appropriate Option:

1.    Addition of Digits
2.    Factorial of a Number
3.    Length of string
4.    String Comparison
5.    String Concatenation
6.    Check For Palindrome
7.    Find Substring.
8.    Run Command
9.    Disconnect And Exit5
Your Choice:5
Enter any First String:Abc

Enter any Second String:Xyz
Output from server:AbcXyz

SELECT Appropriate Option:

1.    Addition of Digits
2.    Factorial of a Number
3.    Length of string
4.    String Comparison
5.    String Concatenation
6.    Check For Palindrome
7.    Find Substring.
8.    Run Command
9.    Disconnect And Exit6
Your Choice:6
Enter any string:malyalam
Output from server:1
Strings is not palindrome

SELECT Appropriate Option:

1.    Addition of Digits
2.    Factorial of a Number
3.    Length of string
4.    String Comparison
5.    String Concatenation
6.    Check For Palindrome
7.    Find Substring.
8.    Run Command
9.    Disconnect And Exit7
Your Choice:7
Enter any string:intelcoreigen

Enter Start Index:2

Enter End Index:5
Output from server:telc

SELECT Appropriate Option:

1.    Addition of Digits
2.    Factorial of a Number
3.    Length of string
4.    String Comparison
5.    String Concatenation
6.    Check For Palindrome
7.    Find Substring.
8.    Run Command
9.    Disconnect And Exit9

*/

Leave a comment

C Program for Hashing Linear Probing with chaining without Replacement

C - Program to Implement Hashing , handle collision  using linear probing with chaining  without replacement


#include <stdio.h>
#include <conio.h>
#define SIZE 10              /* size of the hash table*/
#define FALSE 0
#define TRUE 1
#define h(x) x%SIZE         /*hashing function */
void insert( int data[],int flag[],int chain[],int x);
int search(int data[],int flag[],int chain[],int x);
void print(int data[],int flag[],int chain[]);

void main()
 {
    int data[SIZE],flag[SIZE],chain[SIZE],i,j,x,op,loc;
    /* array data[]  - is a hash table
    array flag[]  - if flag[i] is 1 then the ith place of the hash
              table is filled
    array chain[]- for chaining of synonyms */
    for(i=0;i<SIZE;i++) /* initialize */
       {
        flag[i]=FALSE;
        chain[i]=-1;
       }
    clrscr();
    do
       {
        flushall();
        printf("\n\n1)Insert\n2)Search\n3)Print\n4)Quit");
        printf("\nEnter Your Choice : ");
        scanf("%d",&op);
        switch(op)
           {
            case 1: printf("\n Enter a number to be inserted:");
                scanf("%d",&x);
                insert(data,flag,chain,x);
                break;
            case 2: printf("\n Enter a number to be searched :");
                scanf("%d",&x);
                if((loc=search(data,flag,chain,x))==-1)
                    printf("\n****Element not found****");
                else
                    printf("\n***Found at the location=%d",loc);
                break;
            case 3: print(data,flag,chain);
                break;
           }
    }while(op!=4);
  }

void insert( int data[],int flag[],int chain[],int x)
{
    int i=0,j,start;
    start=h(x); /*hashed location*/
    /*locate the beginning of the chain*/
    while(flag[start] && i<SIZE)
       {
        if(data[start]%SIZE==x%SIZE)
            break;
        i++;
        start=(start+1)%SIZE;
       }
    if(i==SIZE)
       {
        printf("\n***hash table is full****");
        return;
       }
    /*go to the end of chain */
    while(chain[start]!=-1)
        start=chain[start];
    /*locate an empty place for the current data */
    j=start;
    while(flag[j] && i<SIZE)
      {
        j=(j+1)%SIZE;
        i=i+1;
      }
    if(i==SIZE)
      {
        printf("\n***hash table is full****");
        return;
      }
    data[j]=x;   /*store the data */
    flag[j]=TRUE;
    if(j!=start)
        chain[start]=j; /*set the chain */
}

int search(int data[],int flag[],int chain[],int x)
 {
    int i=0,j;
    j=h(x); /*hashed location*/
    /*locate beginning of the chain*/
    while(i<SIZE && flag[j] && data[j]%SIZE !=x%SIZE)
       {
        i++;
        j=(j+1)%SIZE;
       }
    if(!flag[j] || i==SIZE)
        return(-1);
    /*locate the element in the chain */
    while(j!=-1)
       {
        if(data[j]==x)
            return(j);
        j=chain[j];
       }
    return(-1);
}

void print(int data[],int flag[],int chain[])
 {
    int i;
    for(i=0;i<SIZE;i++)
        if(flag[i])
            printf("\n(%d) %d     %d",i,data[i],chain[i]);
        else
            printf("\n(%d) ---    %d",i,chain[i]);
 }

Click Here to Download Source Code with Executable Program

Posted in , , , | 1 Comment

C Program for Depth First Search(DFS) Breadth First Search(BFS)

C - Program to Implement Depth First Search(DFS) and Breadth First Search(BFS)


#include<stdio.h>
#include<conio.h>
#define MAX 30


typedef struct node
{
  int vertex;
  struct node *next;
}node;

typedef struct q
{
  int r,f;
  int data[MAX];
}q;


node *g[20];                                     //heads of linked list
int n;                                           //no. of nodes
int visited[MAX];

void readgraph();                //create adjacency list
void insert(int vi,int vj);                    //insert edge(vi,vj) in graph
void bfs(int i);
void dfs(int i);
void enquee(q *,int);
int dequee(q *);
int empty(q *);
int full(q *);

void main()
{
  int i,k;
  clrscr();
  do
  {
    printf("\n\n1.CREATE A GRAPH\n2.DFS\n3.BFS\n4.QUIT.");
    printf("\nEnter your choice::");
    scanf("%d",&k);
    switch(k)
    {
      case 1:
            readgraph();
            break;
      case 2:
            for(i=0;i<n;i++)
                visited[i]=0;
            printf("\nEnter the start node::");
            scanf("%d",&i);
            dfs(i);
            break;
      case 3:
            printf("\nEnter the start node::");
            scanf("%d",&i);
            bfs(i);
            break;

    }
   }while(k!=4);
}

void readgraph()
{
  int i,j,vi,vj,no;


  printf("\nEnter the no. of vertices::");
  scanf("%d",&n);
  for(i=0;i<n;i++)                //initialise g to NULL
    g[i]=NULL;

  printf("\nEnter the no. of edges::");         //insert edges to graph g
  scanf("%d",&no);
  for(i=0;i<no;i++)
  {
    printf("\nEnter the edge(u,v)::");
    scanf("%d%d",&vi,&vj);
    insert(vi,vj);
    insert(vj,vi);

  }

}

void insert(int vi,int vj)
{
  node *p,*q;
  q=(node*)malloc(sizeof(node));         //acquire memory for new node
  q->vertex=vj;
  q->next=NULL;


  if(g[vi]==NULL)
    g[vi]=q;                               //insert node in linked list
  else
  {
    p=g[vi];
    while(p->next!=NULL)                       //go to end of linked list
    p=p->next;
    p->next=q;
  }
}

void dfs(int i)
{
 node *p;
 p=g[i];
 visited[i]=1;
 printf("\nVisit->%d",i);
 while(p!=NULL)
 {
   i=p->vertex;
   if(!visited[i])
    dfs(i);
   p=p->next;
  }
}

void bfs(int v)
{
  int i,w;
  q x;
  node *p;
  x.r=x.f=-1;                        //initialise quee
  for(i=0;i<n;i++)
    visited[i]=0;

  enquee(&x,v);
  printf("\nVisit->%d",v);
  visited[v]=1;
  while(!empty(&x))
  {
    v=dequee(&x);
    //insert all unvisited vertices of v into quee
    for(p=g[v];p!=NULL;p=p->next)
    {
    w=p->vertex;
    if(visited[w]==0)
    {
        enquee(&x,w);
        visited[w]=1;
        printf("\nVisit->%d",w);
    }
     }
  }
}


int empty(q *p)
{
  if(p->r==-1 &&p->f==-1)
    return(1);
  return(0);
}

int full(q *p)
{
  if(p->r==MAX-1)
    return(1);
  return(0);

}

void enquee(q *p,int x)
{

  if(p->r==-1)
  {
    p->r=p->f=1;
  }
  else
    p->r=p->r+1;
  p->data[p->r]=x;
}

int dequee(q *p)
{
  int x;
  x=p->data[p->f];
  if(p->f==p->r)
    p->r=p->f=-1;
  else
    p->f=p->f+1;
  return(x);
}

Click Here to Download Source Code with Executable Program.

Posted in , | 1 Comment

C - Program to Implement Singly Linked List (SLL)

C - Program to Implement Singly Linked List (SLL)

 

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
typedef struct node
   { int data;
     struct node *next;
   }node;

node *create();
node *insert_b(node *head,int x);
node *insert_e(node *head,int x);
node *insert_in(node *head,int x);
node *delete_b(node *head);
node *delete_e(node *head);
node *delete_in(node *head);
node *reverse(node *head);
void printreverse(node *head);
void print(node *head);

void main()
{ int op,op1,x;
  node *head=NULL;
  clrscr();
  do
    {
      printf("\n\n1)create\n2)Insert\n3)Delete\n4)Display Reverse ");
      printf("\n5)Revert the SLL\n6)Display\n7)Quit");
      printf("\nEnter your Choice:");
      scanf("%d",&op);
      switch(op)
       { case 1:head=create();break;
     case 2:printf("\n\t1)Beginning\n\t2)End\n\t3)In between");
        printf("\nEnter your choice : ");
        scanf("%d",&op1);
        printf("\nEnter the data to be inserted : ");
        scanf("%d",&x);
        switch(op1)
         {  case 1: head=insert_b(head,x);
                break;
            case 2: head=insert_e(head,x);
                break;
            case 3: head=insert_in(head, x);
                break;
          }
        break;
     case 3:printf("\n\t1)Beginning\n\t2)End\n\t3)In between");
        printf("\nEnter your choice : ");
        scanf("%d",&op1);
        switch(op1)
         {  case 1:head=delete_b(head);
               break;
            case 2:head=delete_e(head);
               break;
            case 3:head=delete_in(head);
               break;
          }
         break;
     case 4:printreverse(head);break;
     case 5:head=reverse(head);
        print(head);
        break;
     case 6:print(head);break;
       }
    }while(op!=7);
}

void printreverse(node *head)
  {
    if(head !=NULL)
       {
      printreverse(head->next);
      printf("%d   ",head->data);
       }
  }
node *create()
{ node *head,*p;
  int i,n;
  head=NULL;
  printf("\n Enter no of data:");
  scanf("%d",&n);
  printf("\nEnter the data:");
  for(i=0;i<n;i++)
   {
     if(head==NULL)
     p=head=(node*)malloc(sizeof(node));
     else
       {
     p->next=(node*)malloc(sizeof(node));
     p=p->next;
       }
       p->next=NULL;
       scanf("%d",&(p->data));
   }
 return(head);
}

node *insert_b(node *head,int x)
{   node *p;
    p=(node*)malloc(sizeof(node));
    p->data=x;
    p->next=head;
    head=p;
    return(head);
}

node *insert_e(node *head,int x)
{   node *p,*q;
    p=(node*)malloc(sizeof(node));
    p->data=x;
    p->next=NULL;
    if(head==NULL)
       return(p);
    //locate the last node
    for(q=head;q->next!=NULL;q=q->next)
    ;
    q->next=p;
    return(head);
}

node *insert_in(node *head,int x)
{   node *p,*q;
    int y;
    p=(node*)malloc(sizeof(node));
    p->data=x;
    p->next=NULL;
    printf("\Insert after which number ? : ");
    scanf("%d",&y);
    //locate the lthe data 'y'
    for(q=head ; q != NULL && q->data != y ; q=q->next)
    ;
    if(q!=NULL)
      {
    p->next=q->next;
    q->next=p;
      }
    else
       printf("\nData not found ");
    return(head);
}
node *delete_b(node *head)
{
  node *p,*q;
  if(head==NULL)
     {
    printf("\nUnderflow....Empty Linked List");
    return(head);
     }
  p=head;
  head=head->next;
  free(p);
  return(head);

}
node *delete_e(node *head)
{
  node *p,*q;
  if(head==NULL)
     {
    printf("\nUnderflow....Empty Linked List");
    return(head);
     }
  p=head;
  if(head->next==NULL)
     { // Delete the only element
       head=NULL;
       free(p);
       return(head);
     }
//Locate the last but one node
   for(q=head;q->next->next !=NULL;q=q->next)
   ;
   p=q->next;
   q->next=NULL;
   free(p);
   return(head);
}
node *delete_in(node *head)
{
  node *p,*q;
  int x,i;
  if(head==NULL)
     {
    printf("\nUnderflow....Empty Linked List");
    return(head);
     }
  printf("\nEnter the data to be deleted : ");
  scanf("%d",&x);
  if(head->data==x)
     { // Delete the first element
       p=head;
       head=head->next;
       free(p);
       return(head);
     }
//Locate the node previous to one to be deleted
   for(q=head;q->next->data!=x && q->next !=NULL;q=q->next )
   ;
   if(q->next==NULL)
     {
       printf("\nUnderflow.....data not found");
       return(head);
     }
   p=q->next;
   q->next=q->next->next;
   free(p);
   return(head);
}

void print(node *head)
{ node *p;
 printf("\n\n");
 for(p=head;p!=NULL;p=p->next)
  printf("%d  ",p->data);
}

node *reverse(node *head)
  { node *p,*q,*r;
    p=NULL;
    q=head;
    r=q->next;
    while(q!=NULL)
      {
    q->next=p;
    p=q;
    q=r;
    if(q!=NULL)
       r=q->next;
      }
   return(p);
  }


 Click Here to Download Source Code with Executable Program

Posted in | Leave a comment