staticvoid client(int port) throws Exception { // Get a connection from the server
InetAddress lh = InetAddress.getLocalHost();
InetSocketAddress isa
= new InetSocketAddress(lh, port); int connectFailures = 0; boolean result = false;
SocketChannel sc = SocketChannel.open(); for (;;) { try {
result = sc.connect(isa); break;
} catch (java.net.ConnectException e) {
connectFailures++; if (connectFailures > 30) thrownew RuntimeException("Cannot connect"); Thread.currentThread().sleep(100);
sc = SocketChannel.open();
}
} if (result) {
System.err.println("Connected");
} else { // Only happens when server and client are on separate machines
System.err.println("Connection pending...");
connectFailures = 0; while (!result) { try {
result = sc.finishConnect(); if (!result)
System.err.println("Not finished"); Thread.sleep(50);
} catch (java.net.ConnectException e) { Thread.sleep(100);
connectFailures++; if (connectFailures > 30) thrownew RuntimeException("Cannot finish connecting");
}
}
System.err.println("Finished connecting");
}
ByteBuffer bb = ByteBuffer.allocateDirect(1024); if (sc.read(bb) < 0) thrownew RuntimeException("Failed to read from server"); if (bb.get(0) != 42) thrownew RuntimeException("Read wrong byte from server");
System.err.println("Read from server");
sc.close();
}
publicstaticvoid main(String[] args) throws Exception {
Server server = new Server();
server.start(); try {
client(server.port());
} finally {
server.interrupt();
server.finish(2000);
}
}
}
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.0 Sekunden
(vorverarbeitet am 2026-10-11)
¤
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.