Created
December 19, 2012 22:45
-
-
Save nitsanw/4341270 to your computer and use it in GitHub Desktop.
Ping utility
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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