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

  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;



}







0 Comments