import java.io.*; import java.net.*; public class EchoServer0b { public static void main(String[] args) throws IOException { ServerSocket serverSocket = null; PrintWriter toSocket = null; BufferedReader fromSocket = null; try { serverSocket = new ServerSocket(4444); } catch (IOException e) { System.err.println("!COULD NOT LISTEN ON PORT 4444"); System.exit(1); } while (true){ Socket clientSocket = null; try { System.out.println("!LISTENING ON PORT 4444... "); // 2) clientSocket = serverSocket.accept(); System.out.println("!NEW CONNECTION"); } catch (IOException e) { System.err.println("!ACCEPT FAILED"); System.exit(1); } toSocket = new PrintWriter(clientSocket.getOutputStream(), true); fromSocket = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); String inputLine = ""; System.out.println("!WAITING FOR MESSAGE..."); // 4) inputLine = fromSocket.readLine(); System.out.println("SENDING ECHO"); // 5) toSocket.println("Echo... " + inputLine); } System.out.println("!CLOSING CLIENT SOCKET"); System.out.println(); clientSocket.close(); toSocket.close(); fromSocket.close(); } } }