Technocrat

  • Home

Sunday, May 3, 2015

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

  }

}




  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
  •  WhatsApp
Email ThisBlogThis!Share to TwitterShare to Facebook
Newer Post Older Post Home

0 comments:

Post a Comment

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)
  • ▼  2015 (92)
    • ►  October (4)
    • ►  September (3)
    • ►  August (3)
    • ►  July (9)
    • ►  June (9)
    • ▼  May (20)
      • Print the Truth Table
      • Simple Interest
      • Print the Multiplication Table
      • Find the Greatest of Three Numbers
      • Find Greatest number using Conditional Operator
      • Print 1 to 10 using For Loop
      • Print 1 to 10 using While Loop
      • Swap two Numbers
      • Sum of two numbers
      • Print "Hello, World!" without semicolon
      • Print "Hello, World!"
      • Accept Name, Age from User and Print them on the S...
      • Normalization
      • Earthquake - Do's and Don'ts
      • Now Send mp3 via WhatsApp for Windows Phone
      • WhatsApp getting ready for Windows 10
      • Addition of two numbers on Server sent from Client...
      • Concatenation of two strings sent from Client on t...
      • UDP Client-Server Program (Command Line) using C
      • UDP Client-Server Program using C
    • ►  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