From: Sam Duncan Date: 2012-08-27T07:31:13+09:00 Subject: Re: Threading methods & args On 08/27/2012 10:05 AM, bar hofesh wrote: > Well, this is my simple port scanner > Instead of actually Threading the scan it just prints: > "C:\Users\admin>C:\Ruby193\bin\ruby.exe > C:\Users\admin\Desktop\port-scanner.rb > choose host:" > why ? > > > > require 'socket' > > def scanner > print "choose host: " > arg1 = gets.chomp > print "choose starting port: " > arg2 = gets.to_i > print "choose ending port: " > arg3 = gets.to_i > while arg2 <= arg3 > begin > s = TCPSocket.new(arg1, arg2) # arg1 = host, arg2 = sport, arg3 = > eport > if s > puts "Port #{arg2} is open!" > end > rescue > puts "Port #{arg2} is closed!" > end > arg2 += 1 > > end > end > > Thread.start {scanner} > *) You only launch one thread *) You launch your whole program in that one thread *) Your main thread doesn't wait for the thread to do its work *) You probably want to use Thread#new rather than Thread#start This is not at all good, but works along the lines of what you are doing here. Obviously there is a LOT of code missing. #=============================================================================== #!/usr/bin/env ruby require 'socket' require 'logger' def scanner log = Logger.new(STDOUT) print "choose host: " host = gets.chomp print "choose starting port: " start_port = gets.to_i print "choose ending port: " end_port = gets.to_i puts "Scanning #{host} from #{start_port} to #{end_port}" threads = [] (start_port..end_port).each {|port| threads << Thread.new { begin s = TCPSocket.new(host, port) if s log.info { "Port #{port} is open!" } end rescue log.info { "Port #{port} is closed!" } ensure s.close if s end } } threads.each { |thread| thread.join } end scanner #=============================================================================== Hope that helps/ gets you started =] Sam