# a server program to connect 2 or more clients together. # by Mathieu Bouchard require "fcntl" require "socket" class IO def nonblock=flag bit = Fcntl::O_NONBLOCK fcntl(Fcntl::F_SETFL, (fcntl(Fcntl::F_GETFL) & ~bit) | if flag then bit else 0 end) end # does not work with any ruby version, due to a bug. see below. def read_at_most n s="" k=1<<(Math.log(n)/Math.log(2)).to_i while k>0 unless k+s.length>n puts "trying #{k}" (s << read(k)) rescue Errno::EWOULDBLOCK end k>>=1 end s end # this one works but is slow. def bugfree_read_at_most n s="" (s << (read 1) while s.length