#!/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, '')

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


#this tells the server at 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

start_time = Time.now
  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"
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