Object
FreeUdpPortIterator
- All Implemented Interfaces:
Iterator<DatagramChannel>
Iterates over free UDP ports available in the system and binds to first found.
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionboolean
hasNext()
next()
Find next port which is available and bind to it.withNetworkInterfaceAddress
(InetAddress address) Searches for ports available only on a particular network interfacewithSocketConfigurator
(ThrowingConsumer<DatagramSocket, IOException> socketConfigurator) After port is found it will be binded already by theFreeUdpPortIterator
so that no other component will steal it.Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface Iterator
forEachRemaining, remove
-
Constructor Details
-
FreeUdpPortIterator
public FreeUdpPortIterator(int startPort)
-
-
Method Details
-
withNetworkInterfaceAddress
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 theFreeUdpPortIterator
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 interfaceIterator<DatagramChannel>
-
next
Find next port which is available and bind to it.- Specified by:
next
in interfaceIterator<DatagramChannel>
- Returns:
DatagramChannel
which is attached to found port.
-