Skip to content

Instantly share code, notes, and snippets.

@nitsanw
Created December 19, 2012 22:45
Show Gist options
  • Save nitsanw/4341270 to your computer and use it in GitHub Desktop.
Save nitsanw/4341270 to your computer and use it in GitHub Desktop.
Ping utility
// Server loop
...
accepted = serverSocket.accept();
accepted.socket().setTcpNoDelay(true);
accepted.configureBlocking(false);
serverSocket.close();
while (!Thread.interrupted()) {
buffy.clear();
do {
if (accepted.read(buffy) == -1)
return;
} while (buffy.position() == 0);
buffy.flip();
accepted.write(buffy);
}
// Client loop
...
bb.position(0);
bb.limit(messageSize);
do {
sc.write(bb);
} while (bb.hasRemaining());
// receive
bb.clear();
int bytesRead = 0;
do {
bytesRead += sc.read(bb);
} while (bytesRead < messageSize);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment