Skip to main content

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

*/



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

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

  }

}




Comments

Popular posts from this blog

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 Rectangle { protected: float length, breadth; public: Rectangle(): length(0.0), breadth(0.0) { cout<<"Enter length: "; cin>>length; cout<<"Enter breadth: "; cin>>breadth; } }; /* Area class is derived from base class Rectangle. */ class Area : public Rectangle { public: float calc() { return length*breadth; } }; /* Perimeter class is derived from base class Rectangle. */ class Perimeter : public Rectangle { public: float calc() { return 2*(length+breadth); } }; int main() { cout<<"Enter data for first rectangle to find area.\n"; Area a; cout<<"Area = "<

C program for Unit Conversion

/* Convert Celsius to Fahrenheit */ #include<stdio.h> void main() {     float c,f;     printf("Enter the temperature in Celcius: ");     scanf("%f",&c);     f = c * 9/5 + 32;     printf("Temperature in Farenheit: %f",f); } /* Convert Farenheit to Celcius */ #include<stdio.h> void main() {     float c,f;     printf("Farenheit : ");     scanf("%f",&f);     c = (f - 32) * 5/9;     printf("Celcius: %f",c);  } /*Convert Meter to Feet*/ #include<stdio.h> void main() {     float m,f;     printf("Meter : ");     scanf("%f",&m);     f = 3.2808399 * m;     printf("Feet: %f",f); } /* Convert Feet to Meter */ #include<stdio.h> void main() {     float m,f;     printf("Feet : ");     scanf("%f",&f);     m = f / 3.2808399;     printf("Meter: %f",m); } /*Convert Km to M/cm/mm*/ #include<stdio.h> void main() {   float km;   char v;   float