-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathITDragonAIOClient.java
More file actions
52 lines (45 loc) · 1.58 KB
/
Copy pathITDragonAIOClient.java
File metadata and controls
52 lines (45 loc) · 1.58 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
package IO.AIO;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousSocketChannel;
import java.util.Random;
public class ITDragonAIOClient implements Runnable {
private static Integer PORT = 8888;
private static String IP_ADDRESS = "127.0.0.1";
private AsynchronousSocketChannel asynSocketChannel;
public ITDragonAIOClient() throws Exception {
asynSocketChannel = AsynchronousSocketChannel.open(); // 打开通道
}
public void connect() {
asynSocketChannel.connect(new InetSocketAddress(IP_ADDRESS, PORT)); // 创建连接
// 和NIO一样
}
public void write(String request) {
try {
asynSocketChannel.write(ByteBuffer.wrap(request.getBytes())).get();
ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
asynSocketChannel.read(byteBuffer).get();
byteBuffer.flip();
byte[] respByte = new byte[byteBuffer.remaining()];
byteBuffer.get(respByte); // 将缓冲区的数据放入到 byte数组中
System.out.println(new String(respByte, "utf-8").trim());
} catch (Exception e) {
e.printStackTrace();
}
}
public void run() {
while (true) {
}
}
public static void main(String[] args) throws Exception {
for (int i = 0; i < 10; i++) {
ITDragonAIOClient myClient = new ITDragonAIOClient();
myClient.connect();
new Thread(myClient, "myClient").start();
String[] operators = { "+", "-", "*", "/" };
Random random = new Random(System.currentTimeMillis());
String expression = random.nextInt(10) + operators[random.nextInt(4)] + (random.nextInt(10) + 1);
myClient.write(expression);
}
}
}