Skip to content

Instantly share code, notes, and snippets.

@ejcer
Created April 23, 2016 16:23
Show Gist options
  • Save ejcer/7a453b604be3ab2161cbe0506d091b1a to your computer and use it in GitHub Desktop.
Save ejcer/7a453b604be3ab2161cbe0506d091b1a to your computer and use it in GitHub Desktop.
try {
long startTime = System.currentTimeMillis();
int port = 8000;
InetAddress server = InetAddress.getByAddress("8.8.8.8".getBytes());
DatagramSocket socket = new DatagramSocket(port);
byte[] buf = new byte[1024];
DatagramPacket ping = new DatagramPacket(buf, buf.length, server, port);
try {
socket.send(ping);
socket.setSoTimeout(1000);
DatagramPacket response = new DatagramPacket(new byte[1024], 1024);
socket.receive(response);
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