-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSelectorClient.java
More file actions
37 lines (31 loc) · 948 Bytes
/
Copy pathSelectorClient.java
File metadata and controls
37 lines (31 loc) · 948 Bytes
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
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.util.Date;
import java.lang.NumberFormatException;
import java.io.IOException;
import java.net.InetSocketAddress;
public class SelectorClient{
static ByteBuffer buffer = ByteBuffer.allocateDirect(9);
public static void main(String[] args) throws IOException{
int port = SelectorServer.SERVER_PORT;
if(args.length>0){
try{
port = Integer.parseInt(args[0]);
}catch(NumberFormatException nfe){
nfe.printStackTrace();
}
}
SocketChannel sc = SocketChannel.open(new InetSocketAddress(SelectorServer.SERVER_HOST, port));
long time = 0;
int readLen = 0;
while((readLen = sc.read(buffer)) != -1){
System.out.print("+" + readLen);
};
System.out.println();
buffer.flip();
time = buffer.getLong();
System.out.println("Received Server Time = " + new Date(time));
// close the channel? or close the socket?
sc.close();
}
}