Thread: To find out time elapsed for receiving a message at client's end in socket prg

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    5

    To find out time elapsed for receiving a message at client's end in socket prg

    Hi,

    I want to calculate the time elapsed in receiving a message at client side. Here is what i am trying to do :
    assuming buffer size for receiving is 500 bytes

    timer start
    1) Send a packet (say 500 bytes) from client to Server.
    2) Server receives the packet and sends it back to the client.
    3) Client Receives the packet back.'
    timer stop
    (Note that i am not concerned with how many steps server takes to send message back, that is not in our control since i am using TCP protocol)
    So far I have tried following things.

    1) time() function it basically gives time in seconds, so I have to discard it because here execution time should be in milliseconds, and I was always getting 0 seconds with this.
    2)clock() function, it tells us the no. of cycles it takes for execution and then if u divide it by CLOCKS_PER_SEC, you can get the time elapsed, but in my scenario it was not working, always giving 0 seconds.
    3) gettimeofday(&tv , NULL) : I used this and am getting time in microseconds, but my problem is , it is giving me weird results i.e sometimes I get 500 usec, sometimes 1000 usec, sometimes 5000 usec and sometimes 10000 usec also. I can' t trust so much of variations .

    There is nothing much in code, it is just a client-server socket prg code implementing TCP protocol with loopback port.(127.0.0.1).

    I hope I made myself clear in defining my problem , I want some good function, which can tell me the time elapsed in what i am trying to do.

    thanks in advance

    aditya

  2. #2
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    So, you are attempting to remake ICMP ping on a TCP socket? Sounds very suspiciously like homework.

  3. #3
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    In any case, if you want to calculate time, gettimeofday() is just fine. Don't know if clock() will work, since most of the stuff won't be controlled by the CPU, but the network card.

    The numbers you provide don't sound that much weird to me. It is 0.5 - 10 msec. A +- 10 msec for a server/client type of code might not be strange

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by adityanahan View Post
    3) gettimeofday(&tv , NULL) : I used this and am getting time in microseconds, but my problem is , it is giving me weird results i.e sometimes I get 500 usec, sometimes 1000 usec, sometimes 5000 usec and sometimes 10000 usec also. I can' t trust so much of variations .
    Post the code you are using for the timer. I would use a millisecond timer (eg, ntp_gettime if available) instead of a microsecond timer.

    Also, there can easily be this much variation in times with networking.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Nov 2009
    Posts
    5
    I am posting part of the code from client side.

    Client makes socket and connects to server
    -----------------------------------------------------------------------------------------------------------------
    Code:
    gettimeofday(&tv1,NULL);
    	if(send(sock,echoString,echoStringLen,0) != echoStringLen)  //sending starts here
    		printf(" send failed ");
    	
    	totalBytesRcvd =0;
    	printf("Received: ");
    	int recv_count=0;
    	while(totalBytesRcvd < echoStringLen)
    	{
    		
    		if((bytesRcvd=recv(sock,echoBuffer,RCVBUFSIZE-1,0))<=0) //receiving from server
    		  printf("received failed ");
    		 printf("bytes received = %d \n",bytesRcvd);
    		   totalBytesRcvd += bytesRcvd;
    		  echoBuffer[bytesRcvd]='\0';
    		  puts(echoBuffer);
    		 recv_count++;	
    	}
    	gettimeofday(&tv2,NULL);
            totalT=(tv2.tv_sec-tv1.tv_sec)+(tv2.tv_usec-tv1.tv_usec);
           	printf("recv count = %d and time taken = %ld microseconds \n",recv_count,totalT);
    ----------------------------------------------------------------------------------------------------------------------
    thanks

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Code:
    totalT=(tv2.tv_sec-tv1.tv_sec)+(tv2.tv_usec-tv1.tv_usec);
    This is a little wrong, but much of the time the result will be correct. If the transmission took 3 seconds, tho, using this equation that would only count as 3 microseconds.

    Also, if the start is at 999999998 usecs and the end at 5 usecs, you will get a negative value. Plus one for the extra second Get it?

    Presuming it will never actually be more than one second, you need to add a few more steps:

    if (tv2.secs > tv1.secs) subtract 2.usecs from 1.usecs
    else subtract 1 from 2


    Make sense? This way you always have a positive number.
    Last edited by MK27; 01-20-2010 at 11:00 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Registered User
    Join Date
    Nov 2009
    Posts
    5
    @MK27 thanks for ur response..

    But I did not get what you are trying to say..Can u clarify in detail.

    Code:
      This is a little wrong, but much of the time the result will be correct. If the transmission took 3 seconds, tho, using this equation that would only count as 3 microseconds.
    How can a seconds be shown as microseconds.??I don't understand this.

    Code:
    Also, if the start is at 999999998 usecs and the end at 5 usecs, you will get a negative value. Plus one for the extra second  Get it?
    Why are you saying this..I presume the tv2.secs should be always greater than tv1.secs .

    I think I don't understand how gettimeofday works.. Can you explain me how it works.

    thanks a lot

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    gettimeofday works like a clock with hours and minutes, but instead of hours and minutes it has seconds and microseconds. 4:05 is after 3:57, tho 5 is less than 57.

    totalT is a count of microseconds. (tv2.tv_sec-tv1.tv_sec) is a count of seconds. You need to multiply that number by one million if you want to do it that way -- but don't, because then the second error in your equation will become much more drastic. As is, it is wrong, but it doesn't matter because at most it will add 1 usec to the count.

    Actually what I wrote in that last post is a little wrong too,* it should have been:

    if (tv2.secs > tv1.secs) subtract tv1.usecs from 1000000 then add tv2.usecs
    else subtract tv1.usecs from tv2.usecs


    Applying that to a normal clock (3:57 and 4:05), you would say 4 is greater than 3, so subract 57 from 60 (=3) then add 5. If it were 4:05 and 4:20, you would just subtract 5 from 20.

    *and I used nanoseconds (999999998) instead of microseconds (999998), whoops.
    Last edited by MK27; 01-20-2010 at 11:23 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    Registered User
    Join Date
    Nov 2009
    Posts
    5
    I got your point, but I still have a doubt.

    Since tv2 is after tv1, therefore tv2.sec > tv1.sec (this condition should be always true) Correct me if I am wrong on this.

    So according to me we should always check tv.usec condition like
    if(tv1.usec > tv2.usec)
    1000000-tv1.usec + tv2.usec;
    else
    tv2.usec-tv1.usec ;

    Wht do u think about this MK27

  10. #10
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by adityanahan View Post
    I got your point, but I still have a doubt.

    Since tv2 is after tv1, therefore tv2.sec > tv1.sec (this condition should be always true) Correct me if I am wrong on this.
    No. If it is 2:30, and five minutes go by, what hour is it? Still 2. But the minutes have changed.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  11. #11
    Registered User
    Join Date
    Nov 2009
    Posts
    5
    hey MK27,

    Thanks a lot. I figured out what I was doing wrong and resolved the issue.
    basically to get the time elapsed, you have to multiply seconds by 1000000 and then add the microseconds to it.

    time elapsed = (tv2.sec -tv1.sec)*1000000 + (tv2.usec-tv1.usec);

    it doesn't matter if tv2.usec is smaller or greater than tv1.usec. Because tv2.sec will always be greater than tv1.sec so signs are taken care of by this statement.

    I was looking into what exactly tv.sec returns and what it returns is # of seconds elapsed since Jan 1 ,1970.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. The Timing is incorret
    By Drew in forum C++ Programming
    Replies: 5
    Last Post: 08-28-2003, 04:57 PM
  2. socket newbie, losing a few chars from server to client
    By registering in forum Linux Programming
    Replies: 2
    Last Post: 06-07-2003, 11:48 AM
  3. inputting time in separate compilation
    By sameintheend01 in forum C++ Programming
    Replies: 6
    Last Post: 03-13-2003, 04:33 AM
  4. calculating user time and time elapsed
    By Neildadon in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2003, 06:00 PM