Skip to content

Instantly share code, notes, and snippets.

@ejcer
Created April 22, 2016 17:48
Show Gist options
  • Save ejcer/c4a5e04eacc1745b46f7ed7b3ebdbabd to your computer and use it in GitHub Desktop.
Save ejcer/c4a5e04eacc1745b46f7ed7b3ebdbabd to your computer and use it in GitHub Desktop.
try {
int port = 8000;
InetAddress address = InetAddress.getByName("8.8.8.8");
DatagramSocket socket = new DatagramSocket();
socket.setSoTimeout(2000);
socket.connect(address, port);
for(int i = 0; i < NUM_TRIALS; i++){
String str = "PING " + i + " \n";
byte[] buf = new byte[1024];
buf = str.getBytes();
DatagramPacket ping = new DatagramPacket(buf, buf.length, address, port);
try {
long startTime = System.currentTimeMillis();
socket.send(ping);
DatagramPacket response = new DatagramPacket(new byte[1024], 1024);
socket.receive(response);
System.out.println("response received");
long endTime = System.currentTimeMillis();
pingTimes.add(endTime - startTime);
} catch (IOException e) {
e.printStackTrace();
}
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (SocketException e) {
e.printStackTrace();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment