Forum Xamarin.Forms

Announcement:

The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

access ntp server

AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

Hi friends.
There is an example how to get datetime from a ntp server in XF?
thanks
Alessandro

Posts

  • MitchMilamMitchMilam USMember ✭✭✭

    @AlessandroCaliaro I found a really easy to use add-on for NodaTime:
    https://github.com/mj1856/NodaTime.NetworkClock

    Except for the fact that it mostly doesn't work with Xamarin Forms. The issue seems to be Windows Phone, which doesn't support System.Net.Sockets, which you have to have to communicate with the NTP server. iOS and Android don't seem to have an issue.

    NodaTime.NetworkClock is just a wrapper around this StackOverflow post showing how to query an NTP server.

    If you can find a Sockets PCL that allows you to communicate on all three, you might be able to write it yourself. See this forum conversation for information about that topic.

    Good luck.

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    thanks @MitchMilam . I've tried to download NodaTime package from nuget and to use it in Android, but I don't see the namespace when I try to write some code. I don't know if it can be used in Xamarin

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    @MitchMilam sorry, NodatimeNamespaces appears in my code, but I'm not able to have it working.
    I've used your stack overflow link. In Android it works.
    Thanks!

    Alessandro

  • MitchMilamMitchMilam USMember ✭✭✭

    @AlessandroCaliaro NodaTime works with Xamarin Forms but that extension to get the NTP time does not seem to work on all platforms.

  • ViniciusSchneiderViniciusSchneider USMember ✭✭

    Thank you very much to do this "little" implementation @MitchMilam . I'm not familiar to use UDP, used TCP all of my life, and your IMPLEMENTED code help me very mutch. Just now I used in my application and works fine, just the line:

    ntpData = await tcs.Task.TimeoutAfter(TimeSpan.FromSeconds(3));

    I needed change to

    ntpData = await tcs.Task;

    Due a compile error here.

    I think, adding:

    tcs.Task.Wait(3000);

    Before the await call, result in same thing. I will test with 10 ms answer timeout to see what happens because the actual proccess are leaving about 200ms.

Sign In or Register to comment.