/** * Returns a free port number on the specified host within the given range, * or -1 if none found. * * @param host name or IP address of host on which to find a free port * @param searchFrom the port number from which to start searching * @param searchTo the port number at which to stop searching * @return a free port in the specified range, or -1 of none found * @deprecated Use <code>findFreePort()</code> instead. It is possible that this * method can return a port that is already in use since the implementation does * not bind to the given port to ensure that it is free. */ @Deprecated public static int findUnusedLocalPort(String host, int searchFrom, int searchTo) { for (int i= 0; i < 10; i++) { int port= getRandomPort(searchFrom, searchTo); try (Socket s= new Socket(host, port)){ } catch (ConnectException e) { return port; } catch (IOException e) { } } return -1; }
/** * Returns a free port number on the specified host within the given range, * or -1 if none found. * * @param host name or IP address of host on which to find a free port * @param searchFrom the port number from which to start searching * @param searchTo the port number at which to stop searching * @return a free port in the specified range, or -1 of none found * @deprecated Use <code>findFreePort()</code> instead. It is possible that this * method can return a port that is already in use since the implementation does * not bind to the given port to ensure that it is free. */ @Deprecated public static int findUnusedLocalPort(String host, int searchFrom, int searchTo) { for (int i= 0; i < 10; i++) { int port= getRandomPort(searchFrom, searchTo); try (Socket s= new Socket(host, port)){ } catch (ConnectException e) { return port; } catch (IOException e) { } } return -1; }