package primer6;

import java.net.*;
import java.io.*;

public class EchoClient {

	public static void main(String[] args) {

		String hostname = "localhost";

		if (args.length > 0)
			hostname = args[0];

		try (Socket theSocket = new Socket(hostname, 7)) {
			BufferedReader networkIn = new BufferedReader(new InputStreamReader(theSocket.getInputStream()));
			PrintWriter out = new PrintWriter(theSocket.getOutputStream());

			BufferedReader userIn = new BufferedReader(new InputStreamReader(System.in));
			System.out.println("Connected to echo server");
			while (true) {
				String theLine = userIn.readLine();
				if (theLine.equals("."))
					break;
				out.println(theLine);
				out.flush();
				System.out.println(networkIn.readLine());
			}

		} catch (IOException ex) {
			System.err.println(ex);
		}

	}

}
