From: Antonin AMAND Date: 2006-06-26T22:58:53+09:00 Subject: Re: Blocking read after select Here is the full code. The goal is to provide a https tunnel. In order to use iCal with ssl socket. The server will listen on 127.0.0.7:8888 and forward all traffic comming from one socket to a ssl socket connected to the distant https server. And back. #! /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) 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 "read s" 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 -- Posted via http://www.ruby-forum.com/.