Technocrat

  • Home
Showing posts with label C - Programming. Show all posts
Showing posts with label C - Programming. Show all posts

Sunday, May 3, 2015

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

 Sumit Kar     May 03, 2015     C - Programming, Networking, Socket Program     No comments   


/* 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);

*/



  /**********************************/

  /* build client address structure */

  /**********************************/



  bzero((char *)&clientAddr, sizeof(clientAddr));

  clientAddr.sin_family = AF_INET;

  clientAddr.sin_addr.s_addr = INADDR_ANY;

  clientAddr.sin_port = htons(0);



/*

  bzero((char *)&clientAddr, sizeof(clientAddr));

  clientAddr.sin_family = AF_INET;

  clientAddr.sin_addr.s_addr = inet_addr(CLIENT_ADDR);

  clientAddr.sin_port = htons(CLIENT_PORT);

*/



  /************************/

  /* create stream socket */

  /************************/



  sd = socket(AF_INET, SOCK_STREAM, 0);

  printf("successfully created stream socket \n");



  /**************************/

  /* bind local port number */

  /**************************/



  bind(sd, (struct sockaddr *) &clientAddr, sizeof(clientAddr));

  printf("bound local port successfully\n");



  /*********************/

  /* connect to server */

  /*********************/



  connect(sd, (struct sockaddr *) &servAddr, sizeof(servAddr));

  printf("connected to server successfully\n");



    /***********************/

    /* send data to server */

    /***********************/



  do{

    printf("Enter 1st number : ");

    scanf("%s", line);



    send(sd, line, strlen(line) + 1, 0);

    printf("data sent (%s)\n", line);  

    printf("Enter 2nd number : ");

    scanf("%s", line);

    send(sd, line, strlen(line) + 1, 0);

    printf("data sent (%s)\n", line);

 

    n=recv(sd, line, MAX_MSG, 0);

    printf("received from server %s\n", line);

  }while(strcmp(line, "quit"));





  printf("closing connection with the server\n");

  close(sd);

}












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



/* tcpServer.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,num1,num2,sum;



  struct sockaddr_in cliAddr, servAddr;

  char line[MAX_MSG],line1[MAX_MSG],line2[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 stream socket */

  /************************/



  sd = socket(AF_INET, SOCK_STREAM, 0);

  printf("successfully created stream socket \n");



  /**************************/

  /* bind local port number */

  /**************************/



  bind(sd, (struct sockaddr *) &servAddr, sizeof(servAddr));

  printf("bound local port successfully\n");





  /********************************/

  /* specify number of concurrent */

  /* clients to listen for        */

  /********************************/



  listen(sd,5);





  while(1) {



    printf("waiting for client connection on port TCP %u\n",SERVER_PORT);



    /*****************************/

    /* wait for client connection*/

    /*****************************/  



    cliLen = sizeof(cliAddr);

    newSd = accept(sd, (struct sockaddr *) &cliAddr, &cliLen);



    printf("received connection from host [IP %s ,TCP port %d]\n",

                 inet_ntoa(cliAddr.sin_addr), ntohs(cliAddr.sin_port));



    /*****************************/

    /* wait for data from client */

    /*****************************/    

 

    do{

      memset(line,0x0,MAX_MSG);

 

      n=recv(newSd, line, MAX_MSG, 0);

      num1=atoi(line);

   

      n=recv(newSd, line, MAX_MSG, 0);

      num2=atoi(line);



   

      sum=num1+num2;



      sprintf(line1,"%d",sum);

   

 

      printf("received from host [IP %s ,TCP port %d] : %s\n",

                 inet_ntoa(cliAddr.sin_addr), ntohs(cliAddr.sin_port), line1);

send(newSd, line1, strlen(line1) + 1, 0);



    }while(abs(strcmp(line, "quit")));





    /**************************/

    /* close client connection*/

    /**************************/  



    printf("closing connection with host [IP %s ,TCP port %d]\n",

                 inet_ntoa(cliAddr.sin_addr), ntohs(cliAddr.sin_port));



    close(newSd);

  }

}




Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
  •  WhatsApp

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

 Sumit Kar     May 03, 2015     C - Programming, Networking, Socket Program     No comments   


/* 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);

*/



  /**********************************/

  /* build client address structure */

  /**********************************/



  bzero((char *)&clientAddr, sizeof(clientAddr));

  clientAddr.sin_family = AF_INET;

  clientAddr.sin_addr.s_addr = INADDR_ANY;

  clientAddr.sin_port = htons(0);



/*

  bzero((char *)&clientAddr, sizeof(clientAddr));

  clientAddr.sin_family = AF_INET;

  clientAddr.sin_addr.s_addr = inet_addr(CLIENT_ADDR);

  clientAddr.sin_port = htons(CLIENT_PORT);

*/



  /************************/

  /* create stream socket */

  /************************/



  sd = socket(AF_INET, SOCK_STREAM, 0);

  printf("successfully created stream socket \n");



  /**************************/

  /* bind local port number */

  /**************************/



  bind(sd, (struct sockaddr *) &clientAddr, sizeof(clientAddr));

  printf("bound local port successfully\n");



  /*********************/

  /* connect to server */

  /*********************/



  connect(sd, (struct sockaddr *) &servAddr, sizeof(servAddr));

  printf("connected to server successfully\n");



    /***********************/

    /* send data to server */

    /***********************/



  do{

    printf("Enter string to send to server : ");

    scanf("%s", line);



    send(sd, line, strlen(line) + 1, 0);

    printf("data sent (%s)\n", line);  

    printf("Enter another string to send to server : ");

    scanf("%s", line);

    send(sd, line, strlen(line) + 1, 0);

    printf("data sent (%s)\n", line);

     n=recv(sd, line, MAX_MSG, 0);

   

      printf("received from server %s\n", line);

  }while(strcmp(line, "quit"));





  printf("closing connection with the server\n");

  close(sd);

}












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



/* tcpServer.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],line1[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 stream socket */

  /************************/



  sd = socket(AF_INET, SOCK_STREAM, 0);

  printf("successfully created stream socket \n");



  /**************************/

  /* bind local port number */

  /**************************/



  bind(sd, (struct sockaddr *) &servAddr, sizeof(servAddr));

  printf("bound local port successfully\n");





  /********************************/

  /* specify number of concurrent */

  /* clients to listen for        */

  /********************************/



  listen(sd,5);





  while(1) {



    printf("waiting for client connection on port TCP %u\n",SERVER_PORT);



    /*****************************/

    /* wait for client connection*/

    /*****************************/  



    cliLen = sizeof(cliAddr);

    newSd = accept(sd, (struct sockaddr *) &cliAddr, &cliLen);



    printf("received connection from host [IP %s ,TCP port %d]\n",

                 inet_ntoa(cliAddr.sin_addr), ntohs(cliAddr.sin_port));



    /*****************************/

    /* wait for data from client */

    /*****************************/    

 

    do{

      memset(line,0x0,MAX_MSG);

 

      n=recv(newSd, line, MAX_MSG, 0);

     strcpy(line1,line);

      n=recv(newSd, line, MAX_MSG, 0);

strcat(line1,line);



 

      printf("received from host [IP %s ,TCP port %d] : %s\n",

                 inet_ntoa(cliAddr.sin_addr), ntohs(cliAddr.sin_port), line1);

send(newSd, line1, strlen(line1) + 1, 0);



    }while(abs(strcmp(line, "quit")));





    /**************************/

    /* close client connection*/

    /**************************/  



    printf("closing connection with host [IP %s ,TCP port %d]\n",

                 inet_ntoa(cliAddr.sin_addr), ntohs(cliAddr.sin_port));



    close(newSd);

  }

}




Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
  •  WhatsApp

UDP Client-Server Program (Command Line) using C

 Sumit Kar     May 03, 2015     C - Programming, Networking, Socket Program     No comments   


/* 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));

  remoteServAddr.sin_family = AF_INET;

  remoteServAddr.sin_addr.s_addr = inet_addr(server);

  remoteServAddr.sin_port = htons(portno);





  /**************************/

  /* create datagram socket */

  /**************************/



  sd = socket(AF_INET,SOCK_DGRAM,0);

  if(sd<0)

          printf("Error opening socket");

  else

          printf("successfully created datagram socket\n");





  do {



    /***********************/

    /* send data to server */

    /***********************/



    printf("Enter data to send : ");

    scanf("%s", msg);



    sendto(sd, msg, strlen(msg)+1, 0, (struct sockaddr *) &remoteServAddr,

sizeof(remoteServAddr));



 

  }while(strcmp(msg, "quit"));



 close(sd);

}









/* udpServer.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>





#define MAX_MSG 100







 main(int argc,char *argv[] ) {

  char server[20];

  int i,sd, rc, n, cliLen,portno;

  struct sockaddr_in cliAddr, servAddr;

  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 *)&servAddr, sizeof(servAddr));

  servAddr.sin_family = AF_INET;

  servAddr.sin_addr.s_addr = inet_addr(server);

  servAddr.sin_port = htons(portno);



  /**************************/

  /* create datagram socket */

  /**************************/





  sd=socket(AF_INET, SOCK_DGRAM, 0);

  printf("datagram socket created succefully\n");

  /**************************/

  /* bind local port number */

  /**************************/



  bind (sd, (struct sockaddr *) &servAddr,sizeof(servAddr));

  printf("successfully bound local address\n");



  printf("waiting for data on port UDP %u\n", portno);







  while(1) {

 

    /* init buffer */



    memset(msg,0x0,MAX_MSG);



    /****************************/

    /* receive data from client */

    /****************************/





    cliLen = sizeof(cliAddr);

    n = recvfrom(sd, msg, MAX_MSG, 0, (struct sockaddr *) &cliAddr, &cliLen);



    printf("from %s: UDP port %u : %s \n",

   inet_ntoa(cliAddr.sin_addr), ntohs(cliAddr.sin_port),msg);







  }



return 0;



}







Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
  •  WhatsApp

UDP Client-Server Program using C

 Sumit Kar     May 03, 2015     C - Programming, Networking, Socket Program     No comments   



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 {



    /***********************/

    /* send data to server */

    /***********************/



    printf("Enter data to send : ");

    scanf("%s", msg);



    sendto(sd, msg, strlen(msg)+1, 0, (struct sockaddr *) &remoteServAddr,

sizeof(remoteServAddr));



 

  }while(strcmp(msg, "quit"));



 close(sd);

}












/* udpServer.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> 








#define MAX_MSG 100


#define SERVER_ADDR "127.0.0.1"


#define SERVER_PORT 9227








 main( ) {


  


  int sd, rc, n, cliLen;


  struct sockaddr_in cliAddr, servAddr;


  char msg[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);





  /**************************/


  /* create datagram socket */


  /**************************/





  sd=socket(AF_INET, SOCK_DGRAM, 0);


  printf("datagram socket created succefully\n");





  /**************************/


  /* bind local port number */


  /**************************/





  bind (sd, (struct sockaddr *) &servAddr,sizeof(servAddr));


  printf("successfully bound local address\n");





  printf("waiting for data on port UDP %u\n", SERVER_PORT);











  while(1) {


    


    /* init buffer */





    memset(msg,0x0,MAX_MSG);





    /****************************/


    /* receive data from client */


    /****************************/  








    cliLen = sizeof(cliAddr);


    n = recvfrom(sd, msg, MAX_MSG, 0, (struct sockaddr *) &cliAddr, &cliLen);





    printf("from %s: UDP port %u : %s \n", 


  inet_ntoa(cliAddr.sin_addr), ntohs(cliAddr.sin_port),msg);




  }





return 0;





}






Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
  •  WhatsApp

Friday, April 17, 2015

TCP Echo Server - Echo Client Program using C

 Sumit Kar     April 17, 2015     C - Programming, Networking, Socket Program     No comments   


/* 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 stream socket */

  /************************/



  sd = socket(AF_INET, SOCK_STREAM, 0);

  printf("successfully created stream socket \n");



  /**************************/

  /* bind local port number */

  /**************************/



  bind(sd, (struct sockaddr *) &servAddr, sizeof(servAddr));

  printf("bound local port successfully\n");





  /********************************/

  /* specify number of concurrent */

  /* clients to listen for        */

  /********************************/



  listen(sd,5);





  while(1) {



    printf("waiting for client connection on port TCP %u\n",SERVER_PORT);



    /*****************************/

    /* wait for client connection*/

    /*****************************/  



    cliLen = sizeof(cliAddr);

    newSd = accept(sd, (struct sockaddr *) &cliAddr, &cliLen);



    printf("received connection from host [IP %s ,TCP port %d]\n",

                 inet_ntoa(cliAddr.sin_addr), ntohs(cliAddr.sin_port));



    /*****************************/

    /* wait for data from client */

    /*****************************/    

 

    do{

      memset(line,0x0,MAX_MSG);

 

      n=recv(newSd, line, MAX_MSG, 0);

      line[n]='\n';

   

      printf("received from host [IP %s ,TCP port %d] : %s\n",

                 inet_ntoa(cliAddr.sin_addr), ntohs(cliAddr.sin_port), line);

   

      strcpy(line_r,line);

      send(newSd, line_r, strlen(line_r) + 1, 0);

      printf("data echoed (%s)\n", line_r);



    }while(abs(strcmp(line, "quit")));





    /**************************/

    /* close client connection*/

    /**************************/  



    printf("closing connection with host [IP %s ,TCP port %d]\n",

                 inet_ntoa(cliAddr.sin_addr), ntohs(cliAddr.sin_port));



    close(newSd);

  }

}





--------------------------------------------------------------------------------------------





/* tcpechoClient.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],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);

*/



  /**********************************/

  /* build client address structure */

  /**********************************/



  bzero((char *)&clientAddr, sizeof(clientAddr));

  clientAddr.sin_family = AF_INET;

  clientAddr.sin_addr.s_addr = INADDR_ANY;

  clientAddr.sin_port = htons(0);



/*

  bzero((char *)&clientAddr, sizeof(clientAddr));

  clientAddr.sin_family = AF_INET;

  clientAddr.sin_addr.s_addr = inet_addr(CLIENT_ADDR);

  clientAddr.sin_port = htons(CLIENT_PORT);

*/



  /************************/

  /* create stream socket */

  /************************/



  sd = socket(AF_INET, SOCK_STREAM, 0);

  printf("successfully created stream socket \n");



  /**************************/

  /* bind local port number */

  /**************************/



  bind(sd, (struct sockaddr *) &clientAddr, sizeof(clientAddr));

  printf("bound local port successfully\n");



  /*********************/

  /* connect to server */

  /*********************/



  connect(sd, (struct sockaddr *) &servAddr, sizeof(servAddr));

  printf("connected to server successfully\n");



    /***********************/

    /* send data to server */

    /***********************/



  do{

    printf("Enter string to send to server : ");

    scanf("%s", line);



    send(sd, line, strlen(line) + 1, 0);

    printf("data sent (%s)\n", line);

    n=recv(sd, line_r, MAX_MSG, 0);

      line_r[n]='\n';

 

      printf("received from server [IP %s ,TCP port %d] : %s\n",

                 inet_ntoa(servAddr.sin_addr), ntohs(servAddr.sin_port), line_r);

  }while(strcmp(line, "quit"));





  printf("closing connection with the server\n");

  close(sd);

}



Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
  •  WhatsApp

Thursday, November 27, 2014

Binary Search in C using Recursion

 Sumit Kar     November 27, 2014     Algorithm, C - Programming, Data Structure     No comments   


#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(flag==0)
printf("Number is not found.");
else
printf("Number is found at %d", mid+1);
}


Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
  •  WhatsApp

Wednesday, November 12, 2014

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

 Sumit Kar     November 12, 2014     Algorithm, C - Programming     No comments   



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("\n The resultant Matrix : \n");
for(i=1;i<=n;i++)
{
printf("\n");
for(j=1;j<=n;j++)
printf("%d\t",w[i][j]);
}

getch();

}






Output:





Enter the no. of vertices : 5

Enter the weights : w[1][1]= 0
w[1][2]= 3
w[1][3]= 8
w[1][4]= 0
w[1][5]= 4
w[2][1]= 0
w[2][2]= 0
w[2][3]= 0
w[2][4]= 1
w[2][5]= 7
w[3][1]= 0
w[3][2]= 4
w[3][3]= 0
w[3][4]= 0
w[3][5]= 0
w[4][1]= 2
w[4][2]= 0
w[4][3]= 5
w[4][4]= 0
w[4][5]= 0
w[5][1]= 0
w[5][2]= 0
w[5][3]= 0
w[5][4]= 6
w[5][5]= 0

0 3 8 999 4
999 0 999 1 7
999 4 0 999 999
2 999 5 0 999
999 999 999 6 0
The resultant Matrix :

0 3 8 4 4
3 0 6 1 7
7 4 0 5 11
2 5 5 0 6
8 11 11 6 0











Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
  •  WhatsApp

Insertion Sort

 Sumit Kar     November 12, 2014     Algorithm, C - Programming     No comments   



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 the elements:


8 6 5 1 65


Sorted list in ascending order: 1 5 6 8 65





Conclusion:




Insertion sort is a simple sorting algorithm that
builds the final sorted array (or list) one item at a time. It is much less
efficient on large lists than more advanced algorithms such as quicksort,
heapsort, or merge sort.


Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
  •  WhatsApp

Prim's Algorithm

 Sumit Kar     November 12, 2014     Algorithm, C - Programming     No comments   

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("Enter node value: ");
scanf("%d",&n);
printf("Enter the adjacency matrix: \n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
scanf("%d",&cost[i][j]);
if( cost[i][j]==0 && i!=j )
cost[i][j]=inf;
}
}
printf("Entered Matrix: ");
for(i=1;i<=n;i++)
{
printf("\n");
for(j=1;j<=n;j++)
{
printf("%d\t",cost[i][j]);
}
}
visit[1]=1;
while(p<n)
{
for(i=1,min=999;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(cost[i][j]<min)
{
if (visit[i]!=0)
{
min=cost[i][j];
a=u=i;
b=v=j;
}
}
}
}
if(visit[u]==0 || visit[v]==0)
{
printf("\nEdge %d:(%d-%d) Cost=%d\n",p++,a,b,min);
mincost=mincost+min;
visit[b]=1;
}
cost[a][b]=cost[b][a]=inf;
}
printf("\n Minimum cost=%d",mincost);
getch();
}


Output:
 




Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
  •  WhatsApp

Tuesday, September 9, 2014

C Program to Implement Heap Sort

 Sumit Kar     September 09, 2014     Algorithm, C - Programming     No comments   



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");
for(i=1;i<=n;i++)
{
printf("Enter element [%d]:",i);
scanf("%d",&a[i]);
}


hs(a,n);

printf("The Sorted Elements are:");
for(i=1;i<=n;i++)
{
printf("%d ",a[i]);
}
getch();
}


void hs(int a[],int n)
{
int i,temp;
heap(a,n);
for (i=n;i>1;i--)
{
temp=a[1];
a[1]=a[i];
a[i]=temp;
downheap(a,1,i-1);
}
}


void downheap(int heap[],int root,int leaf)
{
int index,lc,rc,max,temp;
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);
}

}
}


void heap(int a[],int n)
{
int i,index;
index=n/2;
for(i=index;i>=1;i--)
downheap(a,i,n);
}






Output:


Enter the
number of Elements:5


Enter the
elements:


Enter
element [1]:5


Enter
element [2]:8


Enter
element [3]:1


Enter
element [4]:6


Enter
element [5]:65


The
Sorted Elements are:1 5 6 8 65








Conclusion:


The
elements are arranged in a Max Heap. The root element is popped out and the
leaf node is placed in the blank position.


The
program has run correctly.



Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
  •  WhatsApp
Older Posts Home

Pages

  • Home

Trending Now

  • Write a Program to Add two 3x3 Matrix using C
    #include<stdio.h> void main () { int a [ 3 ][ 3 ], b [ 3 ][ 3 ], s [ 3 ][ 3 ], i , j ; printf ( "Enter the values of ...
  • C program for Unit Conversion
    /* Convert Celsius to Fahrenheit */ #include<stdio.h> void main() {     float c,f;     printf("Enter the temperature in Celcius: ...
  • 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> #inc...
  • Write a Program to Print the Truth Table of Basic Gates using C
  • Write a Program to Add two 5x5 Matrix using C
    #include<stdio.h> void main() {   int a[5][5],b[5][5],c[5][5];   int i,j;   for(i=0;i<5;i++)   {     printf("\nEnter elements ...
  • Using the concept of Inheritance write a C++ Program to calculate the area and perimeter of rectangle
    /* C++ Program to calculate the area and perimeter of rectangles using concept of inheritance. */ #include using namespace std; class Re...
  • 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> #inc...
  • 8085 Programming: Exchange the contents of memory locations
    Exchange the contents of memory locations 2000H and 4000H. Program 1: LDA 2000H : Get the contents of memory location 2000H into accumulator...
  • Calculate Depreciation using C
    #include<conio.h> #include<stdio.h> void main () { float sv , pv , dep ; int yos ; clrscr (); printf ( "Enter the pu...
  • 8085 Programming: 1's COMPLEMENT OF A 16-BIT NUMBER
    The 16bit number is stored in C050,C051 The answer is stored in C052,C053   LXI H,C050   MOV A,M   CMA   STA C052   INX H   MOV ...

Blog Archive

  • ▼  2020 (1)
    • ▼  May (1)
      • Automating your deployments using Ansible
  • ►  2015 (92)
    • ►  October (4)
    • ►  September (3)
    • ►  August (3)
    • ►  July (9)
    • ►  June (9)
    • ►  May (20)
    • ►  April (7)
    • ►  March (22)
    • ►  February (7)
    • ►  January (8)
  • ►  2014 (158)
    • ►  November (70)
    • ►  October (6)
    • ►  September (82)
Powered by Blogger.

Categories

C - Programming Java Programming Basic Technology 8085 Assembly Programming For Loop Numerical Methods WhatsApp Algorithm Shell Programming Programming Networking Windows Android C++ Programming CPP If Else Tricky Internet Microsoft Pattern Photography Socket Program News While Loop Array DBMS DS Macro Recursion User Defined Function Conditional Operator Data Structure Durga Puja Earthquake Google Mela Nokia SQL Share Yahoo Airtel Bio Command Prompt Confused Facebook Finance Firefox Ganges Graph HokKolorob Input Kolkata MCQ Math Matrix NetNeutrality Oracle Religion Search Sumit Kar Survey Switch Case Viral Virus Visual Studio do-while featured featured_slider

Popular Programs

  • Write a Program to Add two 3x3 Matrix using C
    #include<stdio.h> void main () { int a [ 3 ][ 3 ], b [ 3 ][ 3 ], s [ 3 ][ 3 ], i , j ; printf ( "Enter the values of ...
  • C program for Unit Conversion
    /* Convert Celsius to Fahrenheit */ #include<stdio.h> void main() {     float c,f;     printf("Enter the temperature in Celcius: ...
  • 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> #inc...
  • Write a Program to Print the Truth Table of Basic Gates using C
  • Write a Program to Add two 5x5 Matrix using C
    #include<stdio.h> void main() {   int a[5][5],b[5][5],c[5][5];   int i,j;   for(i=0;i<5;i++)   {     printf("\nEnter elements ...
  • Using the concept of Inheritance write a C++ Program to calculate the area and perimeter of rectangle
    /* C++ Program to calculate the area and perimeter of rectangles using concept of inheritance. */ #include using namespace std; class Re...
  • 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> #inc...
  • 8085 Programming: Exchange the contents of memory locations
    Exchange the contents of memory locations 2000H and 4000H. Program 1: LDA 2000H : Get the contents of memory location 2000H into accumulator...
  • Calculate Depreciation using C
    #include<conio.h> #include<stdio.h> void main () { float sv , pv , dep ; int yos ; clrscr (); printf ( "Enter the pu...
  • 8085 Programming: 1's COMPLEMENT OF A 16-BIT NUMBER
    The 16bit number is stored in C050,C051 The answer is stored in C052,C053   LXI H,C050   MOV A,M   CMA   STA C052   INX H   MOV ...

Daily Hits

Copyright © Sumit Kar