Biblioteca Java - Blame information for rev 3
Subversion Repositories:
Rev | Author | Line No. | Line |
---|---|---|---|
3 | mihai | 1 | /* |
2 | * Created on Jan 13, 2007 | ||
3 | * | ||
4 | * TODO To change the template for this generated file go to | ||
5 | * Window - Preferences - Java - Code Style - Code Templates | ||
6 | */ | ||
7 | package lab.scd.net.neblocant1; | ||
8 | |||
9 | import java.io.*; | ||
10 | import java.nio.*; | ||
11 | import java.nio.channels.*; | ||
12 | import java.nio.channels.spi.*; | ||
13 | import java.nio.charset.*; | ||
14 | import java.net.*; | ||
15 | import java.util.*; | ||
16 | |||
17 | /** | ||
18 | * @author mihai | ||
19 | * | ||
20 | * TODO To change the template for this generated type comment go to | ||
21 | * Window - Preferences - Java - Code Style - Code Templates | ||
22 | */ | ||
23 | public class NonBlockingServer2 { | ||
24 | |||
25 | public static void main(String[] args) throws Exception{ | ||
26 | |||
27 | // Create the server socket channel | ||
28 | ServerSocketChannel server = ServerSocketChannel.open(); | ||
29 | // nonblocking I/O | ||
30 | server.configureBlocking(false); | ||
31 | // host-port 8000 | ||
32 | server.socket().bind(new java.net.InetSocketAddress("localhost",8000)); | ||
33 | System.out.println("Server waiting on port 8000"); | ||
34 | // Create the selector | ||
35 | Selector selector = Selector.open(); | ||
36 | // Recording server to selector (type OP_ACCEPT) | ||
37 | server.register(selector,SelectionKey.OP_ACCEPT); | ||
38 | |||
39 | // Infinite server loop | ||
40 | |||
41 | for(;;) { | ||
42 | Thread.sleep(1000); | ||
43 | // Waiting for events | ||
44 | System.err.println("wait for event..."); | ||
45 | selector.select(); | ||
46 | |||
47 | // Get keys | ||
48 | Set keys = selector.selectedKeys(); | ||
49 | Iterator i = keys.iterator(); | ||
50 | System.err.println("keys size="+keys.size()); | ||
51 | // For each keys... | ||
52 | while(i.hasNext()) { | ||
53 | |||
54 | // Obtain the interest of the key | ||
55 | SelectionKey key = (SelectionKey) i.next(); | ||
56 | |||
57 | // Remove the current key | ||
58 | i.remove(); | ||
59 | |||
60 | |||
61 | // if isAccetable = true | ||
62 | // then a client required a connection | ||
63 | if (key.isAcceptable()) { | ||
64 | System.err.println("Key is of type acceptable"); | ||
65 | // get client socket channel | ||
66 | SocketChannel client = server.accept(); | ||
67 | // Non Blocking I/O | ||
68 | client.configureBlocking(false); | ||
69 | // recording to the selector (reading) | ||
70 | client.register(selector, SelectionKey.OP_READ); | ||
71 | continue; | ||
72 | } | ||
73 | |||
74 | // if isReadable = true | ||
75 | // then the server is ready to read | ||
76 | if (key.isReadable()) { | ||
77 | System.err.println("Key is of type readable"); | ||
78 | SocketChannel client = (SocketChannel) key.channel(); | ||
79 | |||
80 | // Read byte coming from the client | ||
81 | int BUFFER_SIZE = 32; | ||
82 | ByteBuffer buffer = ByteBuffer.allocate(BUFFER_SIZE); | ||
83 | try { | ||
84 | client.read(buffer); | ||
85 | |||
86 | } | ||
87 | catch (Exception e) { | ||
88 | // client is no longer active | ||
89 | client.close(); | ||
90 | |||
91 | e.printStackTrace(); | ||
92 | continue; | ||
93 | } | ||
94 | |||
95 | // Show bytes on the console | ||
96 | buffer.flip(); | ||
97 | Charset charset=Charset.forName("ISO-8859-1"); | ||
98 | CharsetDecoder decoder = charset.newDecoder(); | ||
99 | CharBuffer charBuffer = decoder.decode(buffer); | ||
100 | System.out.println(charBuffer.toString()); | ||
101 | continue; | ||
102 | } | ||
103 | |||
104 | |||
105 | } | ||
106 | System.err.println("after while keys size="+keys.size()); | ||
107 | } | ||
108 | } | ||
109 | } |