-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSelectorServer.java
More file actions
70 lines (64 loc) · 2.2 KB
/
Copy pathSelectorServer.java
File metadata and controls
70 lines (64 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import java.nio.ByteBuffer;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.nio.channels.Selector;
import java.nio.channels.SelectionKey;
import java.net.InetSocketAddress;
import java.util.Set;
import java.util.Date;
import java.util.Iterator;
import java.lang.NumberFormatException;
import java.io.IOException;
public class SelectorServer{
public static final int SERVER_PORT = 9999;
public static final String SERVER_HOST = "localhost";
static ByteBuffer buffer = ByteBuffer.allocateDirect(8);
public static void main(String[] args) throws IOException{
int port = SERVER_PORT;
if(args.length>0){
try{
port = Integer.parseInt(args[0]);
}catch(NumberFormatException nfe){
nfe.printStackTrace();
}
}
ServerSocketChannel ssc = ServerSocketChannel.open();
ssc.bind(new InetSocketAddress(SERVER_HOST, port));
ssc.configureBlocking(false); // must be set unblocking mode to register
Selector selector = Selector.open();
ssc.register(selector, SelectionKey.OP_ACCEPT);
System.out.println("Server starting... listening on port " + port);
while(true){
int n = selector.select(); // perform a blocking selection operation
if(n==0){
System.out.print("*");
continue;
}
System.out.println("select() return value: " + n);
Set<SelectionKey> selectedKeys = selector.selectedKeys();
Iterator<SelectionKey> iter = selectedKeys.iterator();
while(iter.hasNext()){
SelectionKey key = iter.next();
if(key.isAcceptable()){
SocketChannel sc = ((ServerSocketChannel)key.channel()).accept(); // non-blocking mode.
if(sc==null){
System.out.print(".");
continue;
}
System.out.println("Receiving connection");
buffer.clear(); // prepare to new round operation
long currentTime = System.currentTimeMillis();
buffer.putLong(currentTime);
buffer.flip(); // prepare for draining.
System.out.println("Writing current time = " + new Date(currentTime));
while(buffer.hasRemaining()){
sc.write(buffer);
}
System.out.println("Time was written.");
sc.close();
}
iter.remove(); // must remove the key after you finished the channel operation.
}
}
}
}