import java.io.*;
import java.net.*;

public class EchoClient {
    public static void main(String[] args) throws IOException {
		
		Socket echoSocket = null;
		PrintWriter out = null;
		BufferedReader in = null;
		
		try {
			String hostName = args[0];
			echoSocket = new Socket(hostName, 7);
			out = new PrintWriter(echoSocket.getOutputStream(), true);
			in = new BufferedReader(new InputStreamReader(
				echoSocket.getInputStream()));
		} catch (UnknownHostException e) {
			System.err.println("unkown host");
			System.exit(1);
		} catch (IOException e) {
			System.err.println("Couldn't get I/O for "
				+ "the connection to host");
			System.exit(1);
		} catch (ArrayIndexOutOfBoundsException aiobe) {
			System.err.println("wrong usage: enter hostname");
			System.exit(1);
		}
		
		// establish an input stream to read from the standard input
		BufferedReader input = new BufferedReader(
			new InputStreamReader(System.in));
		String userInput;
		while ((userInput = input.readLine()) != null) {
			
			// writer line to output stream
			out.println(userInput);
			out.flush();
			
			// print received echo result
			System.out.println("echo: " + in.readLine());
		}
		
		// close streams and socket
		out.close();
		in.close();
		input.close();
		echoSocket.close();
    }
}


