Module id.xfunction

Class FreeUdpPortIterator

Object
FreeUdpPortIterator
All Implemented Interfaces:
Iterator<DatagramChannel>

public class FreeUdpPortIterator extends Object implements Iterator<DatagramChannel>
Iterates over free UDP ports available in the system and binds to first found.
  • Constructor Details

    • FreeUdpPortIterator

      public FreeUdpPortIterator(int startPort)
  • Method Details

    • withNetworkInterfaceAddress

      public FreeUdpPortIterator withNetworkInterfaceAddress(InetAddress address)
      Searches for ports available only on a particular network interface
      Parameters:
      address - address assigned to the network interface over which ports to iterate
    • withSocketConfigurator

      public FreeUdpPortIterator withSocketConfigurator(ThrowingConsumer<DatagramSocket,IOException> socketConfigurator)
      After port is found it will be binded already by the FreeUdpPortIterator so that no other component will steal it. It creates a problem that socket options which can be changed only before socket is connected are not available anymore.

      When socket configurator is set it is called before socket bind. It allows users to set socket options as required.

    • hasNext

      public boolean hasNext()
      Specified by:
      hasNext in interface Iterator<DatagramChannel>
    • next

      public DatagramChannel next()
      Find next port which is available and bind to it.
      Specified by:
      next in interface Iterator<DatagramChannel>
      Returns:
      DatagramChannel which is attached to found port.