Created
April 22, 2016 17:48
-
-
Save ejcer/c4a5e04eacc1745b46f7ed7b3ebdbabd to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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