import java.net.*; public class EchoServer { public static void main(String[] args) { if (args.length!=1) { System.out.println("java EchoServer "); System.exit(0); } int port=0; try { port=Integer.parseInt(args[0]); } catch (Exception ex) { System.out.println("java EchoServer "); System.exit(0); } EchoServer server = new EchoServer(port); } public EchoServer(int port) { try { // Server Socket auf bestimmtem Port oeffnen: DatagramSocket sock = new DatagramSocket(port); byte[] data = new byte[1]; // Datenstruktur fuer dem Empfang eines UDP Paketes anlegen DatagramPacket pack = new DatagramPacket(data, data.length); System.out.println("Echo Server ready at port: "+port); while (true) { // warten auf UDP Pakete von clients sock.receive(pack); // Antwort aus dem request zusammenbauen DatagramPacket reply = new DatagramPacket(pack.getData(), pack.getLength(), pack.getAddress(), pack.getPort()); // und weg damit ... sock.send(reply); } } catch (Exception ex) { System.out.println("Encountered unexpected exception: "+ex); } } }