/** * Binds <code>socket</socket> to <code>address</code>. A port is * chosen from this port range. If the port range is [0,0], then a * free port is chosen by the OS. * * @throws IOException if the bind operation fails, or if the * socket is already bound. */ public int bind(ServerSocket socket, InetAddress address, int backlog) throws IOException { int start = random(); int port = start; do { try { socket.bind(new InetSocketAddress(address, port), backlog); return port; } catch (BindException e) { } port = succ(port); } while (port != start); throw new BindException("No free port within range"); }
/** * Binds <code>socket</socket> to <code>address</code>. A port is * chosen from this port range. If the port range is [0,0], then a * free port is chosen by the OS. * * @throws IOException if the bind operation fails, or if the * socket is already bound. */ public int bind(Socket socket, InetAddress address) throws IOException { int start = random(); int port = start; do { try { socket.bind(new InetSocketAddress(address, port)); return port; } catch (BindException e) { } port = succ(port); } while (port != start); throw new BindException("No free port within range"); }