01: //***** ServerSocket
02: 
03: import java.net.Socket;
04: import java.net.ServerSocket;
05: import java.io.IOException;
06: import java.io.BufferedReader;
07: import java.io.InputStreamReader;
08: import java.io.PrintWriter;
09: public class ReverseServer {
10:     public static void main(String[ ] args) {
11:         try {
12:             // Open a server socket on the designated port and
13:             // allow up to max_conn connections at a time.
14:             ServerSocket ssock = new ServerSocket(port, max_conn);
15:         
16:             // Indefinitely wait for and handle clients.
17:             while (true) {
18:                Socket sock = ssock.accept(); // accept() blocks
19: 
20:                // Read the request string from the client
21:                InputStreamReader isr = 
22:                  new InputStreamReader(sock.getInputStream());
23:                BufferedReader  in = new BufferedReader(isr);
24:                String req = in.readLine(); // blocks
25: 
26:                // Produce the response string.
27:                String res = 
28:                  new StringBuffer(req).reverse().toString();
29:                // Write the response to the client.
30:                PrintWriter out = 
31:                  new PrintWriter(sock.getOutputStream());
32:                out.println(res);
33:                out.flush();
34:                // Cleanup.
35:                sock.close();  // closes streams
36:             }
37:         }
38:         catch(IOException e) { System.err.println(e); }
39:     }
40:     private static final int port = 9876;
41:     private static final int max_conn = 100;
42: }