Skip to main content

Posts

Showing posts with the label C - Programming

Addition of two numbers on Server sent from Client [TCP] using C

/* tcpClient.c */ #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <netdb.h> #include <stdio.h> #include <unistd.h> #include <string.h> #include <strings.h> #define MAX_MSG 100 #define SERVER_ADDR "127.0.0.1" #define CLIENT_ADDR "127.0.0.1" #define SERVER_PORT 3786 #define CLIENT_PORT 8229  main () {   int sd, rc, i,n;   struct sockaddr_in clientAddr, servAddr;   char line[MAX_MSG];   /**********************************/   /* build server address structure */   /**********************************/   bzero((char *)&servAddr, sizeof(servAddr));   servAddr.sin_family = AF_INET;   servAddr.sin_addr.s_addr = inet_addr(SERVER_ADDR);   servAddr.sin_port = htons(SERVER_PORT); /*   bzero((char *)&servAddr, sizeof(servAddr));   servAddr.sin_family = AF_INET;   inet_aton(SERVER_ADDR, &servAddr.sin_addr);   servAddr.sin_port = htons(SERVER_PORT); */   /*****

Concatenation of two strings sent from Client on the Server - [ TCP ] using C

/* tcpClient.c */ #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <netdb.h> #include <stdio.h> #include <unistd.h> #include <string.h> #include <strings.h> #define MAX_MSG 100 #define SERVER_ADDR "127.0.0.1" #define CLIENT_ADDR "127.0.0.1" #define SERVER_PORT 3786 #define CLIENT_PORT 8229  main () {   int sd, rc, i,n;   struct sockaddr_in clientAddr, servAddr;   char line[MAX_MSG];   /**********************************/   /* build server address structure */   /**********************************/   bzero((char *)&servAddr, sizeof(servAddr));   servAddr.sin_family = AF_INET;   servAddr.sin_addr.s_addr = inet_addr(SERVER_ADDR);   servAddr.sin_port = htons(SERVER_PORT); /*   bzero((char *)&servAddr, sizeof(servAddr));   servAddr.sin_family = AF_INET;   inet_aton(SERVER_ADDR, &servAddr.sin_addr);   servAddr.sin_port = htons(SERVER_PORT); */   /*****

UDP Client-Server Program (Command Line) using C

/* udpClient.c */ #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <netdb.h> #include <stdio.h> #include<stdlib.h> #include <unistd.h> #include <string.h> #include <strings.h> #include <sys/time.h> #define MAX_MSG 100  main(int argc,char *argv[] ) {   int i, sd, rc, tempLen, n, portno;   struct sockaddr_in remoteServAddr, tempAddr;   struct sockaddr_in cliAddr;   char server[20];   char msg[MAX_MSG];   if(argc<3)     {          fprintf(stderr,"Usage : %s hostname port\n Press Ctrl+C",argv[0]);           }   portno=atoi(argv[2]);   for(i=0;i<20;i++)      server[i]=argv[1][i];     if(server==NULL)     {           printf("Error, no such host");         }   /**********************************/   /* build server address structure */   /**********************************/   bzero((char *)&remoteServAddr, sizeof(remoteServAddr));   remoteServA

UDP Client-Server Program using C

/* udpClient.c */ #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <netdb.h> #include <stdio.h> #include <unistd.h> #include <string.h> #include <sys/time.h> #define MAX_MSG 100 #define SERVER_ADDR "127.0.0.1" #define SERVER_PORT 9227  main( ) {   int sd, rc, tempLen, n;   struct sockaddr_in cliAddr, remoteServAddr, tempAddr;   char msg[MAX_MSG];   /**********************************/   /* build server address structure */   /**********************************/   bzero((char *)&remoteServAddr, sizeof(remoteServAddr));   remoteServAddr.sin_family = AF_INET;   remoteServAddr.sin_addr.s_addr = inet_addr(SERVER_ADDR);   remoteServAddr.sin_port = htons(SERVER_PORT);   /**************************/   /* create datagram socket */   /**************************/   sd = socket(AF_INET,SOCK_DGRAM,0);   printf("successfully created datagram socket\n");   do {     /

TCP Echo Server - Echo Client Program using C

/* tcpechoServer.c */ #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <netdb.h> #include <stdio.h> #include <unistd.h> #include <strings.h> #include <string.h> #define MAX_MSG 100 #define SERVER_ADDR "127.0.0.1" #define SERVER_PORT 3786  main ( ) {   int sd, newSd, cliLen, n;   struct sockaddr_in cliAddr, servAddr;   char line[MAX_MSG], line_r[MAX_MSG];   /**********************************/   /* build server address structure */   /**********************************/   bzero((char *)&servAddr, sizeof(servAddr));   servAddr.sin_family = AF_INET;   servAddr.sin_addr.s_addr = inet_addr(SERVER_ADDR);   servAddr.sin_port = htons(SERVER_PORT); /*   bzero((char *)&servAddr, sizeof(servAddr));   servAddr.sin_family = AF_INET;   inet_aton(SERVER_ADDR, &servAddr.sin_addr);   servAddr.sin_port = htons(SERVER_PORT); */   /************************/   /* create strea

Binary Search in C using Recursion

#include<stdio.h> void bins ( int lb , int ub , int item , int a []); void main () { int i , n , item , a [ 20 ]; printf ( "Enter the size of an array: " ); scanf ( "%d" , & n ); printf ( "Enter the elements of the array: " ); for ( i = 0 ; i < n ; i ++ ) { scanf ( "%d" , & a [ i ]); } printf ( "Enter the number to be search: " ); scanf ( "%d" , & item ); bins ( 0 , n - 1 , item , a ); } void bins ( int lb , int ub , int item , int a []) { int mid , flag = 0 ; if ( lb <= ub ) { mid = ( lb + ub ) / 2 ; if ( item == a [ mid ]) { flag = flag + 1 ; } else if ( item < a [ mid ]) { return bins ( lb , mid - 1 , item , a ); } else return bins ( mid + 1 , ub , item , a ); } if (

Solve All-Pairs Shortest Path Problem using Floyd–Warshall Algorithm

Program: #include<stdio.h> #include<conio.h> #define inf 999 void main () { int i , j , k , n , w [ 20 ][ 20 ]; clrscr (); printf ( " \n Enter the no. of vertices : " ); scanf ( "%d" , & n ); printf ( " \n Enter the weights : " ); for ( i = 1 ; i <= n ; i ++ ) for ( j = 1 ; j <= n ; j ++ ) { printf ( "w[%d][%d]= " , i , j ); scanf ( "%d" , & w [ i ][ j ]); if ( i != j && ( w [ i ][ j ] == 0 )) w [ i ][ j ] = inf ; } for ( i = 1 ; i <= n ; i ++ ) { printf ( " \n " ); for ( j = 1 ; j <= n ; j ++ ) printf ( "%d \t " , w [ i ][ j ]); } for ( k = 1 ; k <= n ; k ++ ) { for ( i = 1 ; i <= n ; i ++ ) { for ( j = 1 ; j <= n ; j ++ ) { if ( w [ i ][ k ] + w [ k ][ j ] < w [ i ][ j ]) w [ i ][ j ] = w [ i ][ k ] + w [ k ][ j ]; } } } printf ( " \

Insertion Sort

Write a C program to implement Insertion Sort Algorithm Algorithm:  for (c = 1 to  n - 1)   {     d <= c;     while ( d > 0 AND array[d] < array[d-1])     {       t          <= array[d];       array[d]   <= array[d-1];       array[d-1] <= t;       d <= d-1;     }   } Program: #include <stdio.h> void main() {   int n, array[20], c, d, t;   printf("Enter the number of Elements:");   scanf("%d", &n);   printf("Enter %d integers:\n", n);   for (c = 0; c < n; c++)   {     scanf("%d", &array[c]);   }   for (c = 1 ; c <= n - 1; c++)   {     d = c;     while ( d > 0 && array[d] < array[d-1])     {       t          = array[d];       array[d]   = array[d-1];       array[d-1] = t;       d--;     }   }   printf("Sorted list in ascending order:\n");   for (c = 0; c <= n - 1; c++)   {     printf("%d  ", array[c]);   } } Output: Enter the number of Elements:5 Enter 5 integers: Enter t

Prim's Algorithm

Write a C Program to Implement Prim’s Algorithm and find the Minimum Spanning Tree (MST) for the following Graph. Algorithm: visit[1] <- 1; while( p < n )      {      min=999;      for( i<- 1 to n )         {         for( j <- 1 to n )            {             if(cost[i][j] < min)               {               if( visit[i]!= 0 )                 {                 Min <- cost[i][j];                 a <- u <- i;                 b <- v <- j;                 }               }            }         }       if( visit[u] = 0 OR visit[v] = 0 )         {          Print Edge of MST;          mincost <- mincost+min;          visit[b] <- 1;         }      cost[a][b] <- cost[b][a] <- inf;      }     Print Total Minimum Cost; Program: #include<stdio.h> #include<conio.h> #define inf 999 int cost [ 10 ][ 10 ], visit [ 10 ] = { 0 }; int n , p = 1 , i , u , v , j , min , a , b , mincost ; void main () { clrscr (); printf ( "Ente

C Program to Implement Heap Sort

Algorithm: hs( a[], n)  {   heap(a,n);   for (i = n to 1)    {     temp<-a[1];     a[1]<-a[i];     a[i]<-temp;     downheap(a,1,i-1);    }  } heap(a[],n)  {   index <- n/2;   for(i= index to 1)     downheap(a,i,n);  } downheap(heap[], root, leaf)  {   lc<-2*root;   rc<-lc+1;   if(lc<=leaf)    {     max<-heap[lc];     index<-lc;     if(rc<=leaf)     {      if(heap[rc]>max)      {       max<-heap[rc];       index<-rc;      }     }     if(heap[root] < heap[index])      {       temp <- heap[root];       heap[root] <- heap[index];       heap[index] <- temp;       downheap(heap,index,leaf);      }    } } Program #include<stdio.h> #include<conio.h> void heap ( int * , int ); void downheap ( int * , int , int ); void hs ( int * , int ); void main () { int a [ 20 ], i , n ; clrscr (); printf ( "Enter the number of Elements:" ); scanf ( "%d" , & n ); printf ( "Enter the elements: \n &qu