Latest

6/recent/ticker-posts

Write an “Echo Client” and “Echo Server” using UDP to estimate the round trip Time from client to the server. The server should be such that it can accept Multiple connections at any given time

 Write an “Echo Client” and “Echo Server” using UDP in Cto estimate the round trip Time from client to the server. The server should be such that it can accept Multiple connections at any given time.

CODE FOR SERVER:


#include < stdio.h >
#include < sys/types.h >
#include < sys/socket.h >
#include < netinet/in.h >
#include < unistd.h >
#include < netdb.h >
#include < string.h >
#include < arpa/inet.h >
#define MAXLINE 1024

int main()

{
  int sockfd;
  int n;
  socklen_t len;
  char msg[1024];
  struct sockaddr_in servaddr,cliaddr;
  sockfd=socket(AF_INET,SOCK_DGRAM,0);
  bzero(&servaddr,sizeof(servaddr));
  servaddr.sin_family=AF_INET;
  servaddr.sin_addr.s_addr=INADDR_ANY;
  servaddr.sin_port=htons(5035);
  bind(sockfd,(struct sockaddr*)&servaddr,sizeof(servaddr));
  printf("\n\n Successfully Binded");
  printf("\n\n Listening...");
  for(;;)

  {
       printf("\n ");
       len=sizeof(cliaddr);
       n=recvfrom(sockfd,msg,MAXLINE,0,(struct sockaddr*)&cliaddr,&len);
       printf("\n Client's Message : %s\n",msg);
       if(n < 6)
          perror("send error");
       sendto(sockfd,msg,n,0,(struct sockaddr*)&cliaddr,len);
  }
return 0;

}


CODE FOR CLIENT


#include < stdio.h >
#include < stdlib.h >
#include < sys/types.h >
#include < sys/socket.h >
#include < netinet/in.h >
#include < string.h >
#include < arpa/inet.h >
#include < time.h >
#define MAXLINE 1024
int main()
{
  int sockfd;
  int n;
  socklen_t len;
  char sendline[1024],recvline[1024];
  struct sockaddr_in servaddr;
  memset(sendline,' ',sizeof(sendline));
  printf("\n Enter the message : ");
  gets(sendline);
  sockfd=socket(AF_INET,SOCK_DGRAM,0);
  bzero(&servaddr,sizeof(servaddr));
  servaddr.sin_family=AF_INET;
  servaddr.sin_addr.s_addr=inet_addr("127.0.0.1");
  servaddr.sin_port=htons(5035);
  len=sizeof(servaddr);
  clock_t t;
  t=clock();
  sendto(sockfd,sendline,MAXLINE,0,(struct sockaddr*)&servaddr,len);
  n=recvfrom(sockfd,recvline,MAXLINE,0,NULL,NULL);
  t=clock()-t;
  double time_taken = ((double)t)/CLOCKS_PER_SEC; 
  recvline[n]=0;
  printf("\n Server's Echo : %s\n",recvline);
  printf("\n Round trip time=%f sec\n\n",time_taken);
return 0;

}







Post a Comment

0 Comments