01: //**** Reverse client and palindrome detector (see last lecture examples) 02: import java.net.Socket; 03: import java.io.IOException; 04: import java.io.PrintWriter; 05: import java.io.InputStreamReader; 06: import java.io.BufferedReader; 07: public class ReverseClient { 08: public static void main(String[ ] args) { 09: // Must be invoked with server's IP address. 10: if (args.length < 1) { 11: System.err.println("Usage: ReverseClient <IP address>"); 12: return; 13: } 14: try { 15: // Open a socket to the server. 16: Socket sock = new Socket(args[0], port); 17: // Print and send the request string. 18: String req = "This is the way the world ends"; 19: System.out.println("Sent: " + req); 20: PrintWriter out = 21: new PrintWriter(sock.getOutputStream()); 22: out.println(req); 23: out.flush(); 24: // Read and print the response string. 25: InputStreamReader isr = 26: new InputStreamReader(sock.getInputStream()); 27: BufferedReader in = new BufferedReader(isr); 28: String res = in.readLine(); // blocks 29: System.out.println("Received: " + res); 30: // Cleanup. 31: sock.close(); 32: // Expensive, socket-based palindrome checker. 33: if (req.equals(res)) 34: System.out.println("A palindrome!"); 35: else 36: System.out.println("Not a palindrome!"); 37: } 38: catch(IOException e) { System.err.println(e); } 39: } 40: private static final int port = 3333; 41: }