performance - How to calculate time of udp packet sending and receiving -


i have implemented udp packet sender , receiver. want calculate time transaction, sender receiver how time taking..

sender code:

void senderfunc() { /*some other code */ if((s1 = socket(af_inet, sock_dgram, ipproto_udp)) == -1)     error_handler("\nerror: in socket\n");     memset((char *) &me, 0, sizeof(me));     me.sin_family = af_inet;     me.sin_port = port;     if (inet_aton(g_ip, &me.sin_addr)==0)     {         fprintf(stderr, "inet_aton() failed\n");         exit(1);     }     printf("\tick - %d : %s",cntr++,str);     sprintf(b1, "%s",str);  // information in b1 buffer transfer      if(sendto(s1, b1, sizeof(b1),0,(struct sockaddr *) &me,n)==-1)     error_handler("\nerror: in sendto()\n");     close (s1);     return;     } } 

receiver code:

int receiverfunc () {     struct sockaddr_in other, me;     int s2, n, = 1;     char b2[buflen];//, b2[buflen];     s2 = socket(af_inet, sock_dgram,0);     me.sin_family = af_inet;     me.sin_port = port;     me.sin_addr.s_addr = htonl(inaddr_any);     bind(s2,(struct sockaddr *)&me, sizeof(me));     n=sizeof(other);     int incr = 0;     while (i){         recvfrom (s2,b2,buflen,0,(struct sockaddr *) &other, &n);         printf ("\nsubnet 2: tick - %d : %s",incr++, b2);     }     return 0; } 

can please me how can calculate time between transaction suppose sending packet sender receiver. how can calculate time?

thanks best responses forum.. looking best response..

thanks

rahee.

i think need:

1) synchronize client,server same timeserver (ntp), sure have same time.

on client side:

2) current time , send udp package timestart

#include <sys/time.h>  struct timeval timestart; gettimeofday(&timestart, null);  ... //put timestart  buffer ... 

on server side:

3) extract time structure, , compare current time:

#include <sys/time.h>  ... //get timestart buffer ...  struct timeval timeend; gettimeofday(&timeend, null);  long diff_msec = (timestart.tv_sec  - timeend.tv_sec) / 1000 + (timestart.tv_usec  - timeend.tv_usec); 

Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -