|
@ -0,0 +1,42 @@ |
|
|
|
|
|
package verteiltesysteme.socket.timecounter; |
|
|
|
|
|
|
|
|
|
|
|
import java.io.BufferedReader; |
|
|
|
|
|
import java.io.InputStreamReader; |
|
|
|
|
|
import java.net.DatagramPacket; |
|
|
|
|
|
import java.net.DatagramSocket; |
|
|
|
|
|
import java.net.InetAddress; |
|
|
|
|
|
import java.text.DateFormat; |
|
|
|
|
|
import java.text.SimpleDateFormat; |
|
|
|
|
|
import java.util.Date; |
|
|
|
|
|
|
|
|
|
|
|
class UDPTimeCounterClient { |
|
|
|
|
|
public static void main(String args[]) throws Exception { |
|
|
|
|
|
String hostname = "192.168.73.158"; |
|
|
|
|
|
int udpPort = 36038; |
|
|
|
|
|
BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in)); |
|
|
|
|
|
DatagramSocket clientSocket = new DatagramSocket(); |
|
|
|
|
|
InetAddress IPAddress = InetAddress.getByName(hostname); |
|
|
|
|
|
byte[] sendData = new byte[1024]; |
|
|
|
|
|
byte[] receiveData = new byte[1024]; |
|
|
|
|
|
DateFormat df = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss"); |
|
|
|
|
|
|
|
|
|
|
|
System.out.println("Bitte geben Sie eine Zahl ein: "); |
|
|
|
|
|
Long number = new Long(inFromUser.readLine()); |
|
|
|
|
|
sendData = number.toString().getBytes(); |
|
|
|
|
|
|
|
|
|
|
|
System.out.println(System.lineSeparator()); |
|
|
|
|
|
System.out.println("Zeit vor Versand = " + System.currentTimeMillis() + " " + df.format(new Date(System.currentTimeMillis()))); |
|
|
|
|
|
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, udpPort); |
|
|
|
|
|
clientSocket.send(sendPacket); |
|
|
|
|
|
System.out.println("Zeit nach Versand = " + System.currentTimeMillis() + " " + df.format(new Date(System.currentTimeMillis()))); |
|
|
|
|
|
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); |
|
|
|
|
|
clientSocket.receive(receivePacket); |
|
|
|
|
|
System.out.println("Zeit nach Empfang = " + System.currentTimeMillis() + " " + df.format(new Date(System.currentTimeMillis()))); |
|
|
|
|
|
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(); |
|
|
|
|
|
} |
|
|
|
|
|
} |