import java.net.*; import java.io.*; // send 1 line from stdin to server, read one line reply // usage: java client localhost port 7 -> echo; 13 -> daytime // uses Stream Readers and Writers, for chars 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]; new cli(host, port); } cli(String host, String port) // tcp/ip version { try { BufferedReader stdin = new BufferedReader( new InputStreamReader(System.in)); Socket sock = new Socket(host, Integer.parseInt(port)); System.err.println("java client " + sock); BufferedReader sin = new BufferedReader( new InputStreamReader(sock.getInputStream())); BufferedWriter sout = new BufferedWriter( new OutputStreamWriter(sock.getOutputStream())); String s; while ((s = stdin.readLine()) != null) { // read cmd sout.write(s); // write to socket sout.newLine(); sout.flush(); // needed String r = sin.readLine(); // read reply System.out.println(host + " got [" + r + "]"); if (s.equals("exit")) break; } sock.close(); } catch (IOException e) { e.printStackTrace(); } } }