// Test hosts used by the channels tests - change these when // executing in a different network. publicstaticfinal String UNRESOLVABLE_HOST = "blah-blah.blah-blah.blah";
private TestUtil() { }
// Repeatedly try random ports until we bind to one. You might be tempted // to do this: // // ServerSocketChannel ssc = ServerSocketChannel.open(); // ssc.socket().bind(new InetSocketAddress(0)); // SocketAddress sa = ssc.socket().getLocalSocketAddress(); // // but unfortunately it doesn't work on NT 4.0. // // Returns the bound port. // staticint bind(ServerSocketChannel ssc) throws IOException {
InetAddress lh = InetAddress.getLocalHost();
Random r = new Random(); for (;;) { int p = r.nextInt((1 << 16) - 1024) + 1024;
InetSocketAddress isa = new InetSocketAddress(lh, p); try {
ssc.socket().bind(isa);
} catch (IOException x) { continue;
} return p;
}
}
// A more convenient form of bind(ServerSocketChannel) that returns a full // socket address. // static InetSocketAddress bindToRandomPort(ServerSocketChannel ssc) throws IOException
{ int p = bind(ssc); returnnew InetSocketAddress(InetAddress.getLocalHost(), p);
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.