Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Find Next Highest Available Port

DZone's Guide to

Find Next Highest Available Port

·
Free Resource
This function loops until the next available port on the local machine is found. This code is not for remote port scanning--it works on your own machine if you need to dynamically find a non-used port, perhaps because you're running large numbers of servers locally or some other edge case.


  def next_available_port(port)
    port_found=false
    until(port_found==true )
      begin
       TCPServer.new(port).close
       port_found=true
      rescue Exception=>e
        puts "PORT exception #{e.message}"
        port+=1
      end
    end
    port
  end
Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}