StanM StanM -4 years ago 147
Ruby Question

Sending UDP Packet to NTP to get a packet back (in ruby)

What I need to do:
Send a packet to pool.ntp.org (and I am assuming I get a packet back automatically).

The following just does not work for me. I have no solid idea of what I am doing, so for now I would be satisfied if I could reach the address and get the packet back. The code below just hangs. Any and all help would be appreciated.

require 'socket'
sock = UDPSocket.new
sock.connect("pool.ntp.org", 123)
sock.recvfrom(10)

Answer Source

The documentation on UDPSocket states:

connect(host, port) 
Connects udpsocket to host:port.

This makes possible to send without destination address.

That means that you may use send(mesg, flags) form of the send, nothing more. You need to send a request message to the NTP server to get a reply

You can see the NTPv4 protocol specification here: RFC 5905

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download