import java.net.*; import java.io.*; // send 1 line from stdin to server, read one line reply // try java cli localhost 7 -> echo; 13 -> daytime public class cli { static String host = "localhost"; static String port = "5194"; public static void main(String[] argv) { if (argv.length > 0) host = argv[0]; if (argv.length > 1) port = argv[1]; cli(host, port); } public static void cli(String host, String port) // tcp/ip version { try { DataInputStream stdin = new DataInputStream(System.in); Socket client = new Socket(host, Integer.parseInt(port)); System.err.println("cli " + client); DataInputStream sin = new DataInputStream(client.getInputStream()); DataOutputStream sout = new DataOutputStream(client.getOutputStream()); String s; while ((s = stdin.readLine()) != null) { // read cmd sout.writeBytes(s); // write to socket sout.writeByte('\n'); String r = sin.readLine(); // read reply System.out.println(host + " reply " + r); if (s.equals("exit")) break; } client.close(); } catch (IOException e) { e.printStackTrace(); } } }