#!/usr/bin/env ruby
require 'socket'

opt = [5, 0].pack("i,i")

sock = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0)
sockaddr = Socket.pack_sockaddr_in(3232, '127.0.0.1')

sock.setsockopt(Socket::SOL_SOCKET, Socket::SO_RCVTIMEO, opt)
puts "sockopt = #{sock.getsockopt(Socket::SOL_SOCKET, Socket::SO_RCVTIMEO).inspect}"

sock.connect(sockaddr)

#this tells the server at 192.168.1.10:3232 to wait 4 seconds before responding, then send some data, then wait another 4 before sending the newline -- so we should get a timeout on our socket -- after having read some of the data
sock.write("4\r\n")

start_time = Time.now
begin
  result = sock.recvfrom(8192)
rescue Errno::EAGAIN
  puts "got timeout -- current state of the code this won't get hit because recvfrom isn't looking for a newline, just reads the first bit of data sent"
end
time = Time.now - start_time

puts "read: #{result.inspect} after: #{time} seconds"
#timeout occured, this read returns after 4 seconds

#note -- this is testing against a server running, sample code for server here: http://pastie.org/314915