import java.net.*; import java.io.*; public class CommandClient { public static void main(String[] args) { if (args.length!=2) { System.out.println("java CommandClient "); 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 EchoServer "); System.exit(0); } CommandClient client = new CommandClient(server, port); } public CommandClient (InetAddress server, int port) { try { // TCP Verbindungsaufbau zum Server: Socket sock = new Socket(server, port); // Ein- und Ausgabestoehme des Sockets holen: InputStream sockIn = sock.getInputStream(); OutputStream sockOut = sock.getOutputStream(); // Ein- und Ausgabe in entsprechendes Format umwandeln: BufferedReader sockReader = new BufferedReader(new InputStreamReader(sockIn)); BufferedWriter sockWriter = new BufferedWriter(new OutputStreamWriter(sockOut)); BufferedReader keyboardReader = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Connection Established"); String command=""; String reply=""; while(!command.equals("quit")) { System.out.print("> "); command=keyboardReader.readLine(); // Befehl auf den Socket schreiben: sockWriter.write(command,0,command.length()); sockWriter.newLine(); // wichtig: sonst wird es weiter gepuffert und nicht verschickt: sockWriter.flush(); // Antwort von Socket lesen: reply=sockReader.readLine(); System.out.println(reply); } // TCP Verbindungsabbau: sock.close(); } catch (Exception ex) { System.err.println("CommandClient: received exception "+ex); } } }