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]
]
)