最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

delphi - How do I reset a buffer of indy UDP client? - Stack Overflow

programmeradmin2浏览0评论

I noticed a little issue with Indy's TIdSNTP: If the timeout is less than actual network delay, when doing DateTime request, the datagram is not received in timeout time;

however this old datagram is kept in some internal buffer and next call to the DateTime receives this old datagram, giving wrong time value.

  var sntp1: TIdSNTP := TIdSNTP.Create;
  sntp1.Host := 'pool.ntp';
  sntp1.ReceiveTimeout := 5;
  sntp1.DateTime;
  Memo1.Lines.Add(Format(' delay: %d', [ round(sntp1.RoundTripDelay * MSecsPerDay) ]));
  Application.ProcessMessages;
  sleep(3000);
  sntp1.ReceiveTimeout := 500;
  sntp1.DateTime;
  Memo1.Lines.Add(Format(' delay: %d', [ round(sntp1.RoundTripDelay * MSecsPerDay) ]));

The question is, how do I properly reset the buffer to clear old pending data, before next call to DateTime ?

I noticed a little issue with Indy's TIdSNTP: If the timeout is less than actual network delay, when doing DateTime request, the datagram is not received in timeout time;

however this old datagram is kept in some internal buffer and next call to the DateTime receives this old datagram, giving wrong time value.

  var sntp1: TIdSNTP := TIdSNTP.Create;
  sntp1.Host := 'pool.ntp.';
  sntp1.ReceiveTimeout := 5;
  sntp1.DateTime;
  Memo1.Lines.Add(Format(' delay: %d', [ round(sntp1.RoundTripDelay * MSecsPerDay) ]));
  Application.ProcessMessages;
  sleep(3000);
  sntp1.ReceiveTimeout := 500;
  sntp1.DateTime;
  Memo1.Lines.Add(Format(' delay: %d', [ round(sntp1.RoundTripDelay * MSecsPerDay) ]));

The question is, how do I properly reset the buffer to clear old pending data, before next call to DateTime ?

Share Improve this question asked Jan 29 at 12:10 SargisSargis 2032 silver badges10 bronze badges 1
  • 1 As a workaround, you may create a new instance of the TIdSNTP before every usage. And free the previous, of course. – mjn Commented Jan 29 at 12:16
Add a comment  | 

1 Answer 1

Reset to default 2

You are reusing the same underlying socket for both TIdSNTP.DateTime queries, which is why the packet is able to be cached inside the socket.

You should re-create the TIdSNTP object each time, or at least close its current socket with TIdSNTP.Binding.CloseSocket() so the next query will create a new socket.

You could try flushing the socket buffer manually, by reading directly from the TIdSNTP.Binding socket until there is nothing left to read. But this is still a race condition if a new packet arrives after you flush but before you perform the next query. So better to just discard the socket altogether to throw away the current buffer.

发布评论

评论列表(0)

  1. 暂无评论