import java.io.*; import java.net.*; public class EchoClient0 { public static void main(String[] args) throws IOException { Socket echoSocket = null; PrintWriter toSocket = null; BufferedReader fromSocket = null; try { // 1) echoSocket = new Socket("localhost", 4444); toSocket = new PrintWriter(echoSocket.getOutputStream(), true); fromSocket = new BufferedReader(new InputStreamReader(echoSocket.getInputStream())); } catch (UnknownHostException e) { System.err.println("Don't know about host: localhost."); System.exit(1); } catch (IOException e) { System.err.println("Couldn't get I/O for the connection to: localhost."); System.exit(1); } BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Message to EchoServer (type): "); String userInput = stdIn.readLine(); if (userInput != null) { // 3) toSocket.println(userInput); } // 6) String serverMessage = fromSocket.readLine(); System.out.println("Message from EchoServer: " + serverMessage); toSocket.close(); fromSocket.close(); stdIn.close(); echoSocket.close(); } }