I can’t seem to get this one working; Why won’t Thread.start start?
# encoding: utf-8
require 'socket'
print "choose host: "
host = gets.chomp
print "choose starting port: "
sport = gets.to_i
print "choose ending port: "
eport = gets.to_i
def scanner (sport, eport, host)
while sport <= eport
begin
s = TCPSocket.new(host, sport)
if s
puts "Port #{sport} is open!"
end
rescue
puts "Port #{sport} is closed!"
end
sport += 1
end
end
Thread.start([scanner]sport, eport, host)
You need to join the worker thread from the main thread. What is happening is the main thread exits which causes the entire process to exit, shutting down the worker thread before it has finished.
You need to wait for the worker thread by joining it after you start it. Look for a function like
Thread.joinor similar in your languages threading api.