boost posix time question

This is a discussion on boost posix time question within the C++ Programming forums, part of the General Programming Boards category; Hello I'm developing server and client applications and I need ping-pong mechanism to get ping time betwen them (time needed ...

  1. #1
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630

    boost posix time question

    Hello

    I'm developing server and client applications and I need ping-pong mechanism to get ping time betwen them (time needed for data to arrive)..

    I've been thinking the best way would be to send
    Code:
    boost::posix_time::microsec_clock::universal_time()
    from server to client, which would then just reply with its own universal_time() ..


    The first problem is I dont know how to store universal_time() as an int/long.. I know I can store it as a string but this would take extra cpu to parse the data each time (with boost posix library)..

    All the data I send is serialized - maybe I could just serialize the
    Code:
    boost::posix_time::ptime
    object and send over?


    The next thing is what if server's time zone is gmt+1 and client's gmt+3? Will that affect the the time if I do the subtraction to get the ping time? If so, what can I do about it?

    Maybe anyone has better solution in mind?

    Thanks a lot for help

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    universal_time() is independent of timezone. It's not independent of inaccurate clocks, though.

    To measure travel time of packets, a peer can only rely on itself. What you do is, you send a very simple packet (ping uses the ICMP protocol and the ICMP_ECHO_REQUEST message) to the server, which responds to it by immediately sending an also very simple response packet (ICMP_ECHO_RESPONSE in ping). Turnaround time should be as close to zero as possible. The client marks the time when it sends, and when it receives the answer. It then has the full time it takes for the request-response cycle. It can repeat it a few times to get an average. It can divide it in half to get the one-way time - on average, that will be accurate.
    If the server needs the time, too, it can either do its own ping or, more efficiently, you can make a pong protocol, i.e. clients sends ECHO(1), server responds with ECHO(2), client responds immediately with ECHO(3), and so on, up to, say, ECHO(7). The client measures between send(1) and rcv(2), send(3) and rcv(4), send(5) and rcv(6), while the server measures between send(2) and rcv(3), send(4) and rcv(5), send(6) and rcv(7).

    Or something like that. But never rely on another computer's clock, unless they are in a time-synchronized environment.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    unless they are in a time-synchronized environment
    I do not know what exsactly you mean by this... by even if 2 computers have "synchronized" their times using some time-synchronization mechanism - difference can still be same order as a trip time between two computers or between them and time-server...

    So I will not relay on these two times for packet trip mesurement... Round-Trip approach is OK
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Time protocols are not simple, but they do manage to minimize the inaccuracy that results from the lag.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630
    Thanks a lot!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using pointers
    By Big_0_72 in forum C Programming
    Replies: 3
    Last Post: 10-28-2008, 07:51 PM
  2. need help in time zone
    By Gong in forum C++ Programming
    Replies: 2
    Last Post: 01-03-2007, 03:44 AM
  3. It's a question of time ...
    By Mandy_C in forum C Programming
    Replies: 4
    Last Post: 04-09-2006, 02:18 PM
  4. time question.
    By InvariantLoop in forum C Programming
    Replies: 5
    Last Post: 02-01-2005, 01:00 PM
  5. inputting time in separate compilation
    By sameintheend01 in forum C++ Programming
    Replies: 6
    Last Post: 03-13-2003, 03:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21