Browse Source

added host input, recording and measurement of timestamps

master
Sebastian Rieger 7 years ago
parent
commit
53f315f897
  1. 23
      VerteilteSysteme-Examples/src/verteiltesysteme/socket/timecounter/UDPTimeCounterClient.java
  2. 1
      VerteilteSysteme-Examples/src/verteiltesysteme/socket/timecounter/UDPTimeCounterServer.java

23
VerteilteSysteme-Examples/src/verteiltesysteme/socket/timecounter/UDPTimeCounterClient.java

@ -11,31 +11,44 @@ import java.util.Date;
class UDPTimeCounterClient { class UDPTimeCounterClient {
public static void main(String args[]) throws Exception { public static void main(String args[]) throws Exception {
String hostname = "192.168.73.158";
String hostname = "";
int udpPort = 36038; int udpPort = 36038;
BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in)); BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
DatagramSocket clientSocket = new DatagramSocket(); DatagramSocket clientSocket = new DatagramSocket();
InetAddress IPAddress = InetAddress.getByName(hostname);
byte[] sendData = new byte[1024]; byte[] sendData = new byte[1024];
byte[] receiveData = new byte[1024]; byte[] receiveData = new byte[1024];
DateFormat df = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss"); DateFormat df = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");
System.out.println("Bitte geben Sie die Server-Adresse ein: ");
hostname = inFromUser.readLine();
InetAddress IPAddress = InetAddress.getByName(hostname);
System.out.println("Bitte geben Sie eine Zahl ein: "); System.out.println("Bitte geben Sie eine Zahl ein: ");
Long number = new Long(inFromUser.readLine()); Long number = new Long(inFromUser.readLine());
sendData = number.toString().getBytes(); sendData = number.toString().getBytes();
System.out.println(System.lineSeparator()); System.out.println(System.lineSeparator());
System.out.println("Zeit vor Versand = " + System.currentTimeMillis() + " " + df.format(new Date(System.currentTimeMillis())));
long timestampStart = System.currentTimeMillis();
System.out.println("Zeit vor Versand = " + timestampStart + " ms, " + df.format(new Date(timestampStart)));
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, udpPort); DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, udpPort);
clientSocket.send(sendPacket); clientSocket.send(sendPacket);
System.out.println("Zeit nach Versand = " + System.currentTimeMillis() + " " + df.format(new Date(System.currentTimeMillis())));
long timestampSent = System.currentTimeMillis();
System.out.println("Zeit nach Versand = " + timestampSent + " ms, " + df.format(new Date(timestampSent)));
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
clientSocket.receive(receivePacket); clientSocket.receive(receivePacket);
System.out.println("Zeit nach Empfang = " + System.currentTimeMillis() + " " + df.format(new Date(System.currentTimeMillis())));
long timestampEnd = System.currentTimeMillis();
System.out.println("Zeit nach Empfang = " + timestampEnd + " ms, " + df.format(new Date(timestampEnd)));
long delay = timestampEnd - timestampStart;
System.out.println("Delay = " + delay + " ms");
System.out.println(System.lineSeparator()); System.out.println(System.lineSeparator());
String result = new String(receivePacket.getData()); String result = new String(receivePacket.getData());
System.out.println("Ausgabe vom Server:" + System.lineSeparator() + result); System.out.println("Ausgabe vom Server:" + System.lineSeparator() + result);
long timestampServer = new Long(result.split(" ")[2]).longValue();
float oneWayDelayEstimate = (timestampEnd - timestampSent) / 2;
float timestampSkew = timestampServer - timestampSent - oneWayDelayEstimate;
System.out.println("Zeitversatz zwischen Server und Client ca. = " + timestampSkew + " ms");
System.out.println(System.lineSeparator()); System.out.println(System.lineSeparator());
clientSocket.close(); clientSocket.close();
} }

1
VerteilteSysteme-Examples/src/verteiltesysteme/socket/timecounter/UDPTimeCounterServer.java

@ -27,6 +27,7 @@ class UDPTimeCounterServer {
int clientSrcPort = receivePacket.getPort(); int clientSrcPort = receivePacket.getPort();
counter += number; counter += number;
sendData = (Long.toString(counter) + " Zeit: " + System.currentTimeMillis() + " " + df.format(new Date(System.currentTimeMillis()))).getBytes(); sendData = (Long.toString(counter) + " Zeit: " + System.currentTimeMillis() + " " + df.format(new Date(System.currentTimeMillis()))).getBytes();
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, clientSrcPort); DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, clientSrcPort);
serverSocket.send(sendPacket); serverSocket.send(sendPacket);

Loading…
Cancel
Save