module Ackintosh::Net::EmptyPort

Constants

VERSION

Public Class Methods

find(protocol = "tcp") click to toggle source

Find a free TCP/UDP port. @param protocol [String] tcp or udp @return port [Fixnum]

# File lib/ackintosh/net/empty_port.rb, line 11
def self.find(protocol = "tcp")
  port = 49152
  begin
    return port unless self.used?(port, protocol)
    port += 1
  end while port <= 65535

  raise "Empty port not found."
end
used?(port, protocol = "tcp") click to toggle source

Is the port used ? @param port @param protocol @return [boolean]

# File lib/ackintosh/net/empty_port.rb, line 25
def self.used?(port, protocol = "tcp")
  class_name = (protocol == "tcp") ? "TCPSocket" : "UDPSocket"
  begin
    s = const_get(class_name).open("localhost", port)
  rescue => e
    return false
  end
  s.close
  true
end