Java Reference
In-Depth Information
LISTING 17.5
Continued
41: if (selKey.isAcceptable()) {
42:
43: // Create a socket connection with the client
44: ServerSocketChannel selChannel =
45: (ServerSocketChannel) selKey.channel();
46: ServerSocket selSocket = selChannel.socket();
47: Socket connection = selSocket.accept();
48:
49: // Handle the finger request
50: handleRequest(connection);
51: connection.close();
52: }
53: }
54: }
55: } catch (IOException ioe) {
56: System.out.println(ioe.getMessage());
57: }
58: }
59:
60: private void handleRequest(Socket connection) throws IOException {
61:
62: // Set up input and output
63: InputStreamReader isr = new InputStreamReader (
64: connection.getInputStream());
65: BufferedReader is = new BufferedReader(isr);
66: PrintWriter pw = new PrintWriter(new
67: BufferedOutputStream (connection.getOutputStream()),
68: false);
69:
70: // Output server greeting
71: pw.println(“Nio Finger Server”);
72: pw.flush();
73:
74: // Handle user input
75: String outLine = null;
76: String inLine = is.readLine();
77:
78: if (inLine.length() > 0) {
79: outLine = inLine;
80: }
81: readPlan(outLine, pw);
82:
83: // Clean up
84: pw.flush();
85: pw.close();
86: is.close();
87: }
88:
89: private void readPlan(String userName, PrintWriter pw) {
17
Search WWH ::




Custom Search