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.net.*; | ||
11 | import java.nio.*; | ||
12 | import java.nio.channels.*; | ||
13 | import java.nio.charset.Charset; | ||
14 | import java.nio.charset.CharsetDecoder; | ||
15 | import java.util.*; | ||
16 | |||
17 | public class NonBlockingServer3 { | ||
18 | private static int port = 8000; | ||
19 | public static void main(String args[]) | ||
20 | throws Exception { | ||
21 | Selector selector = Selector.open(); | ||
22 | |||
23 | ServerSocketChannel channel = | ||
24 | ServerSocketChannel.open(); | ||
25 | channel.configureBlocking(false); | ||
26 | InetSocketAddress isa = new InetSocketAddress(port); | ||
27 | channel.socket().bind(isa); | ||
28 | |||
29 | // Register interest in when connection | ||
30 | channel.register(selector, SelectionKey.OP_ACCEPT); | ||
31 | |||
32 | // Wait for something of interest to happen | ||
33 | while (selector.select() > 0) { | ||
34 | System.err.println("new event happened..."); | ||
35 | // Get set of ready objects | ||
36 | Set readyKeys = selector.selectedKeys(); | ||
37 | Iterator readyItor = readyKeys.iterator(); | ||
38 | |||
39 | // Walk through set | ||
40 | while (readyItor.hasNext()) { | ||
41 | |||
42 | // Get key from set | ||
43 | SelectionKey key = | ||
44 | (SelectionKey)readyItor.next(); | ||
45 | |||
46 | // Remove current entry | ||
47 | readyItor.remove(); | ||
48 | |||
49 | if (key.isAcceptable()) { | ||
50 | // Get channel | ||
51 | ServerSocketChannel keyChannel = | ||
52 | (ServerSocketChannel)key.channel(); | ||
53 | |||
54 | // Get server socket | ||
55 | ServerSocket serverSocket = keyChannel.socket(); | ||
56 | |||
57 | // Accept request | ||
58 | Socket socket = serverSocket.accept(); | ||
59 | |||
60 | // Return canned message | ||
61 | PrintWriter out = new PrintWriter | ||
62 | (socket.getOutputStream(), true); | ||
63 | out.println("Hello, NIO"); | ||
64 | out.close(); | ||
65 | } | ||
66 | if (key.isReadable()) { | ||
67 | |||
68 | SocketChannel client = (SocketChannel) key.channel(); | ||
69 | |||
70 | // Read byte coming from the client | ||
71 | int BUFFER_SIZE = 32; | ||
72 | ByteBuffer buffer = ByteBuffer.allocate(BUFFER_SIZE); | ||
73 | try { | ||
74 | client.read(buffer); | ||
75 | |||
76 | } | ||
77 | catch (Exception e) { | ||
78 | // client is no longer active | ||
79 | client.close(); | ||
80 | |||
81 | e.printStackTrace(); | ||
82 | continue; | ||
83 | } | ||
84 | |||
85 | // Show bytes on the console | ||
86 | buffer.flip(); | ||
87 | Charset charset=Charset.forName("ISO-8859-1"); | ||
88 | CharsetDecoder decoder = charset.newDecoder(); | ||
89 | CharBuffer charBuffer = decoder.decode(buffer); | ||
90 | System.out.println(charBuffer.toString()); | ||
91 | continue; | ||
92 | } | ||
93 | else { | ||
94 | System.err.println("Ooops"); | ||
95 | } | ||
96 | |||
97 | } | ||
98 | } | ||
99 | // Never ends | ||
100 | } | ||
101 | } |