From: Francis Cianfrocca Date: 2007-09-05T09:46:10+09:00 Subject: Re: Socket send functions only allow you to send strings? ------=_Part_10901_15905883.1188953169283 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline On 9/4/07, khaines@enigo.com wrote: > > On Wed, 5 Sep 2007, Ryan Parmeter wrote: > > > As far as I can tell, my only option to write to a UDPSocket is to use > > the send method, which takes a string. I need to send a 32bit integer > > representing the date in a UDP packet and I'd love to use something > > like: > > > > myUDPsocket.send( Time.now.to_i, 0, host, port) > > > > In my situation I can't change the receiving program to accept a string > > or I could do it that way. I can't convert the integer to a string > > either because that would blow my 32 bit requirement. Does anyone have > > any idea? > > A string is just a sequence of bytes. It can be anything. > > In your case, you need to encode a decimal number as returned by > Time.now.to_i into 32 bits. > > myUDPsocket.send( [Time.now.to_i].pack('i') ) > > That may be what you need. > > http://ruby-doc.org/core/classes/Array.html#M002245 > > For more information. > > > Kirk Haines > > > Actually, to support RFC-868, you have to use the same epoch that NTP uses, rather than the Unix epoch. The current time in raw form would be Time.now.to_i + 0x83aa7e80. Pack into 32 bits (taking care to keep the conversion unsigned), convert to network-order as necessary, write a four-byte UDP datagram, and away you go. ------=_Part_10901_15905883.1188953169283--