import java.net.*; public class EchoClient { public static void main(String[] args) { if (args.length!=2) { System.out.println("java EchoClient "); System.exit(0); } int port=0; InetAddress server=null; try { server = InetAddress.getByName(args[0]); port=Integer.parseInt(args[1]); } catch (Exception ex) { System.out.println("java EchoClient "); System.exit(0); } EchoClient client = new EchoClient(server, port); } public EchoClient(InetAddress server, int port) { try { // client socket mit beliebigem freien UDP Port aufmachen DatagramSocket sock = new DatagramSocket(); // neuen thread starten, der auf Antworten wartet Receiver rec = new Receiver(sock); byte[] data = new byte[1]; for (byte i=0; i<10; i++) { data[0]=i; // request Paket zusammenbauen DatagramPacket pack = new DatagramPacket(data, 1, server, port); // und weg damit ... sock.send(pack); System.out.println("Sent ping request sequ : "+i+" at "+System.currentTimeMillis()); Thread.currentThread().sleep(1000); } } catch (Exception ex) { System.out.println("Encountered unexpected exception: "+ex); } System.exit(0); } } // Thread der auf Antworten wartet analog zum server Programm ... class Receiver extends Thread { DatagramSocket sock; Receiver (DatagramSocket s) { sock = s; start(); } public void run() { byte[] data = new byte[1]; DatagramPacket reply = new DatagramPacket(data,1); try { while (true) { sock.receive(reply); System.out.println("Received ping reply sequ: "+(reply.getData())[0]+" at "+System.currentTimeMillis()); } } catch (Exception ex) { System.out.println("Encountered unexpected exception: "+ex); System.exit(0); } } }