Selector selector = Selector.open (); int requests = 0; int responses = 0; while (true) { // we need to read responses from time to time: slightly // increase the timeout with the amount of pending responses // to give a chance to the server to reply. int selres = selector.select (requests - responses + 1);
Set<SelectionKey> selkeys = selector.selectedKeys(); for (SelectionKey key : selkeys) { if (key.isReadable()) {
SocketChannel chan = (SocketChannel)key.channel();
ByteBuffer buf = (ByteBuffer)key.attachment(); try { int x = chan.read(buf); if (x == -1 || responseComplete(buf)) {
System.out.print("_");
key.attach(null);
chan.close();
responses++;
}
} catch (IOException e) {
System.out.println(e);
}
}
} if (requests < NUM) {
System.out.print(".");
SocketChannel schan = SocketChannel.open(destaddr);
requestBuf.rewind(); int c = 0; while (requestBuf.remaining() > 0) {
c += schan.write(requestBuf);
}
schan.configureBlocking(false);
schan.register(selector, SelectionKey.OP_READ, ByteBuffer.allocate(100));
requests++;
} if (responses == NUM) {
System.out.println ("Finished clients"); break;
}
}
server.stop (1);
selector.close();
executor.shutdown ();
}
/* Look for CR LF CR LF */ staticboolean responseComplete(ByteBuffer buf) { int pos = buf.position();
buf.flip(); byte[] lookingFor = newbyte[] {'\r', '\n', '\r', '\n' }; int lookingForCount = 0; while (buf.hasRemaining()) { byte b = buf.get(); if (b == lookingFor[lookingForCount]) {
lookingForCount++; if (lookingForCount == 4) { returntrue;
}
} else {
lookingForCount = 0;
}
}
buf.position(pos);
buf.limit(buf.capacity()); returnfalse;
}
}
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.11 Sekunden
(vorverarbeitet am 2026-06-10)
¤
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.