From 4d205447a9a1d7ecfc7c37ef22beb1c64ece736d Mon Sep 17 00:00:00 2001 From: Sebastian Rieger Date: Sat, 28 Oct 2017 00:18:04 +0200 Subject: [PATCH] initial version of udp ping client --- .../timecounter/UDPTimeCounterClientPing.java | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 VerteilteSysteme-Examples/src/verteiltesysteme/socket/timecounter/UDPTimeCounterClientPing.java diff --git a/VerteilteSysteme-Examples/src/verteiltesysteme/socket/timecounter/UDPTimeCounterClientPing.java b/VerteilteSysteme-Examples/src/verteiltesysteme/socket/timecounter/UDPTimeCounterClientPing.java new file mode 100644 index 0000000..a29a5e8 --- /dev/null +++ b/VerteilteSysteme-Examples/src/verteiltesysteme/socket/timecounter/UDPTimeCounterClientPing.java @@ -0,0 +1,52 @@ +package verteiltesysteme.socket.timecounter; + +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.net.DatagramPacket; +import java.net.DatagramSocket; +import java.net.InetAddress; + +class UDPTimeCounterClientPing { + public static void main(String args[]) throws Exception { + String hostname = ""; + int udpPort = 36038; + BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in)); + DatagramSocket clientSocket = new DatagramSocket(); + byte[] sendData = new byte[1024]; + byte[] receiveData = new byte[1024]; + + System.out.println("Bitte geben Sie die Server-Adresse ein: "); + hostname = inFromUser.readLine(); + InetAddress IPAddress = InetAddress.getByName(hostname); + System.out.println("Bitte geben Sie die Anzahl gewünschter Pings ein: "); + Long pingCount = new Long(inFromUser.readLine()); + Long number = new Long(1); + sendData = number.toString().getBytes(); + + DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); + + long average = 0; + for (long i = 0; i < pingCount; i++) + { + long timestampStart = System.currentTimeMillis(); + DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, udpPort); + clientSocket.send(sendPacket); + receivePacket = new DatagramPacket(receiveData, receiveData.length); + + clientSocket.receive(receivePacket); + + long timestampEnd = System.currentTimeMillis(); + long delay = timestampEnd - timestampStart; + average += delay; + System.out.println("Delay = " + delay + " ms"); + Thread.sleep(1000); + } + System.out.println("Durchschnitt: " + (average / (float)pingCount) + " ms"); + System.out.println(System.lineSeparator()); + + String result = new String(receivePacket.getData()); + System.out.println("Ausgabe vom Server:" + System.lineSeparator() + result); + System.out.println(System.lineSeparator()); + clientSocket.close(); + } +}