while( true ){ // Accept a connection Socket soc = host.accept(); System.out.println( "Connected on port " + soc.getPort() ); // Get a Stream to read from the socket InputStream sis = soc.getInputStream(); BufferedReader in = new BufferedReader( new InputStreamReader( sis ) ); // Get a Stream to write to the socket OutputStream ops = soc.getOutputStream(); PrintStream out = new PrintStream(ops); String input; while( (input=in.readLine()) != null ){ if(input.equals("close")) break; System.out.println( input ); out.println( input ); } // Close the socket connection soc.close(); out.println("Connection Closed."); System.out.println( "Socket closed." ); }