From: ara.t.howard@... Date: 2006-06-26T23:20:36+09:00 Subject: Re: Blocking read after select On Mon, 26 Jun 2006, Antonin AMAND wrote: > Here is the full code. if you make the mod i suggested it works fine: harp:~ > ruby a.rb thread start -609397948/-609397928/-609398078 select... reading s... read: <"11223 @ Mon Jun 26 08:18:09 MDT 2006\n"> select... thread start -609398348/-609398318/-609398498 select... reading s... read: <"11224 @ Mon Jun 26 08:18:10 MDT 2006\n"> select... thread start -609398768/-609398738/-609398918 select... reading s... read: <"11225 @ Mon Jun 26 08:18:11 MDT 2006\n"> select... harp:~ > cat a.rb #! /usr/bin/env/ruby require 'openssl' require 'socket' def set_non_blocking(io) flag = File::NONBLOCK if defined?(Fcntl::F_GETFL) flag |= io.fcntl(Fcntl::F_GETFL) end io.fcntl(Fcntl::F_SETFL, flag) end def get_ssl_socket(host, port) ssl_context = OpenSSL::SSL::SSLContext.new() ssl_context.verify_mode = OpenSSL::SSL::VERIFY_NONE socket = TCPSocket.new(host, port) sslsocket = OpenSSL::SSL::SSLSocket.new(socket, ssl_context) unless ssl_context.verify_mode warn "warning: peer certificate won't be verified this session." end sslsocket.sync_close = true sslsocket.connect sslsocket end ## FIXME: listen on all interfaces -> localhost tunnel = TCPServer.new(8888) tunnel.listen(5) ## fork tester clients in background fork do loop do fork do s = TCPSocket.new 'localhost', 8888 s.puts "#{ $$ } @ #{ Time.now.to_s }" end Process.wait sleep 1 end end while sock = tunnel.accept Thread.start(sock) do |s| ssl_sock = get_ssl_socket("ssl.gwikzone.org", 443) #set_non_blocking ssl_sock.to_io #set_non_blocking s.to_io STDERR.puts "thread start #{Thread.current.object_id}/#{s.object_id}/#{ssl_sock.object_id}" while true STDERR.puts "select..." res = select([s, ssl_sock]) unless res.nil? for rsock in res[0] #delete closed connections if rsock.eof? || ssl_sock.closed? s.close ssl_sock.close Thread.current.terminate end if rsock == s STDERR.puts "reading s..." buf = s.gets STDERR.puts "read: <#{ buf.inspect }>" #buf = s.read unless s.ready? #STDERR.puts "s: \n#{send}" #ssl_sock.write buf unless buf.nil? || buf.empty? end if rsock == ssl_sock STDERR.puts "read ssl" buf = rsock.read STDERR.puts "ssl: \n#{send}" s.write buf unless buf.nil? || buf.empty? end end #for end #unless end #while true end #Thread.start end #while perhaps networking is fubar on your machine? any silly virus software running? regards. -a -- suffering increases your inner strength. also, the wishing for suffering makes the suffering disappear. - h.h. the 14th dali lama