Browse Source

initial version of udp ping client

master
Sebastian Rieger 7 years ago
parent
commit
4d205447a9
  1. 52
      VerteilteSysteme-Examples/src/verteiltesysteme/socket/timecounter/UDPTimeCounterClientPing.java

52
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();
}
}
Loading…
Cancel
Save