Hello everybody!

I started using Godot since 6 month. And I currently do some experiment to see what Godot is able to do.

Here is one of them. I am trying to do a NTP synchronisation for a clock.

How its works:

Function Init(): Send a UDP packet to myself to see the UDP port used by the sender.

Function Synchronise(): Send a NTP request to a NTP server then listen to the UDP port used by the sender to get the anwser.

Init() and Synchronise() works nearly the same.

My problem: Synchronise() is waiting for the NTP anwser but nothing come. It is an infinite waiting because wait() is blocking. Line 41.

I shure that the server give an anwser by using Wireshark.

I hope somebody can help me to understand the problem. I joins the GdScript in case.

Regards.

Foreman21.

I think the problem is that "receiverPort" is never being set (or it is set wrongly from the init()). In fact, you should remove or comment out init() as I think it is just confusing things. I haven't worked with NTP before, but does it operate on a set port? In that case you can just hard-code it. Or find some other way to find it, I don't think sending a UDP packet on a localhost would result in the same number as a live server.

Hi Cybereality! The problem is the port used by a udp request is random.

ReceiverPort is well initialised in the init(). Inside init() I print ReceiverPort once I had an answer.

On wireshark the NTP packet is send from the ReceiverPort and I got and response on the same port from the server.

To resume ntp: Do a request from port ex 50930 to ntp server port 123 Get an answer from port 123 to port 50930.

9 days later

When you execute listen on PacketPeerUDP, it will reject the specified port number.

In this case, it is impossible to know the port number sent from NTPServer in advance. So, this is effectively a refusal to receive. Then again, you don't need to know or set your own port number.

It's more of a Socket thing than a PacketPeerUDP thing, but it will automatically assign you a free port number to use.

This port number will be recorded in the packets that are sent, so that the other party who receives the packet will be able to send it back.

A short code that works is described below. I hope you find it useful.

extends Node2D

const NTP_HOST = "0.nettime.pool.ntp.org"
const NTP_PORT = 123

func _ready():

    var o_peer_udp: PacketPeerUDP = PacketPeerUDP.new()
    var send_packet = PoolByteArray()

    send_packet.append_array([0x1B])
    for n in range(47):
        send_packet.append_array([0x00])

    o_peer_udp.set_dest_address(NTP_HOST, NTP_PORT)
    o_peer_udp.put_packet(send_packet)

    o_peer_udp.wait()

    var recv_packet = o_peer_udp.get_packet()
    
    if recv_packet.size() > 0:
        for n in range(12):
            print(
                "%02X %02X %02X %02X" % [
                    recv_packet[(4 * n) + 0],
                    recv_packet[(4 * n) + 1],
                    recv_packet[(4 * n) + 2],
                    recv_packet[(4 * n) + 3]
                    ]
            )

Thanks awesome. Thanks for sharing, @MizunagiKB

@MizunagiKB This is exactly what I want to do! Awesome !!! Now I understand how to deal with udp and how to put hexadeciamal into. I have all I need to make a ntp synchronized clock! Thanks a lot!

a year later