FileDocCategorySizeDatePackage
EchoClient.javaAPI DocExample1710Mon Apr 17 17:00:42 BST 2000None

EchoClient

public class EchoClient extends Object
EchoClient - simple line-mode echo client. Reads from stdin, writes to console. Talks to a UNIX "echo" server or a surrogate for it (EchoServer.java).
author
Ian Darwin, Learning Tree, Course 471/478 author.
version
Copyright (C) 1995, 1996 Ian F. Darwin

Fields Summary
Constructors Summary
Methods Summary
protected voidconverse(java.lang.String hostname)
Hold one conversation with the named hosts echo server

		Socket sock = null;
		try {
			int i;
			sock = new Socket(hostname, 7);	// echo server.
			BufferedReader stdin = new BufferedReader(
				new InputStreamReader(System.in));
			BufferedReader is = new BufferedReader(
				new InputStreamReader(sock.getInputStream(), "8859_1"));
			PrintWriter os = new PrintWriter(
				new OutputStreamWriter(
					sock.getOutputStream(), "8859_1"), true);

			String line;
			do {
				System.out.print(">> ");
				if ((line = stdin.readLine()) == null)
					break;
				// Do the CRLF ourself since println appends only a \r on
				// platforms where that is the native line ending.
				os.print(line + "\r\n");
				os.flush();
				String reply = is.readLine();
				System.out.print("<< ");
				System.out.println(reply);
			} while (line != null);
		} catch (IOException e) {	// handles all input/output errors
			System.err.println(e);
		} finally {					// cleanup
			try {
				if (sock != null)
					sock.close();
			} catch (IOException ignoreMe) {
				// nothing
			}
		}
	
public static voidmain(java.lang.String[] argv)
Main program: construct an EchoClient object and use its "converse" method to call the Echo server.

		EchoClient c = new EchoClient();
		c.converse(argv.length==1?argv[0]:"localhost");