Repository navigation
Expand file tree
/
Copy pathSocketTest.java
More file actions
130 lines (118 loc) · 5.02 KB
/
Copy pathSocketTest.java
File metadata and controls
130 lines (118 loc) · 5.02 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package com.dz.test;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
/**
* Created by RoyDeng on 17/11/9.
*/
public class SocketTest {
private Logger logger = LoggerFactory.getLogger(SocketTest.class);
private SocketAddress address = new InetSocketAddress("api.groupy.cn", 8888);
/*
11:40:40.326 [main] DEBUG com.roy.test.SocketTest - start
11:40:40.345 [main] DEBUG com.roy.test.SocketTest - readByte 5
11:40:40.345 [main] DEBUG com.roy.test.SocketTest - read 1
11:40:40.345 [main] DEBUG com.roy.test.SocketTest - read 0
11:40:40.345 [main] DEBUG com.roy.test.SocketTest - read 0
11:40:40.345 [main] DEBUG com.roy.test.SocketTest - read 0
11:40:40.345 [main] DEBUG com.roy.test.SocketTest - read 0
*/
@Test
public void testSocket() throws Exception {
logger.debug("start");
Socket socket = new Socket();
socket.connect(address);
byte[] output = new byte[]{(byte) 1, (byte) 0, (byte) 0, (byte) 0, (byte) 0};
socket.getOutputStream().write(output);
byte[] input = new byte[64];
int readByte = socket.getInputStream().read(input);
logger.debug("readByte " + readByte);
for (int i = 0; i < readByte; i++) {
logger.debug("read [" + i + "]:" + input[i]);
}
socket.close();
}
@Test
public void testSocketChannelBlock() throws Exception {
final SocketChannel channel = SocketChannel.open(address);
ByteBuffer output = ByteBuffer.allocate(5);
output.put((byte) 1);
output.putInt(0);
output.flip();
channel.write(output);
logger.debug("write complete, start read");
ByteBuffer input = ByteBuffer.allocate(5);
int readByte = channel.read(input);
logger.debug("readByte " + readByte);
input.flip();
if (readByte == -1) {
logger.debug("readByte == -1, return!");
return;
}
for (int i = 0; i < readByte; i++) {
logger.debug("read [" + i + "]:" + input.get());
}
}
@Test
public void testSocketChannelConcurrent() throws Exception {
final SocketChannel channel = SocketChannel.open(address);
Selector selector = Selector.open();
// 设置channel为非阻塞模式
channel.configureBlocking(false);
// 想Selector注册read消息,一旦可以read了,就会通知到SocketChannel
channel.register(selector, SelectionKey.OP_READ);
ByteBuffer output = ByteBuffer.allocate(5);
output.put((byte) 1);
output.putInt(0);
// 写完数据之后记得调用flip(),否则索引不对,数据会写不进去
output.flip();
channel.write(output);
logger.debug("write complete, start read");
while (true) {
// selectNow()是立即返回结果的,类似的方法有阻塞的select()和select(int timeOutMills)
if (selector.selectNow() > 0) {
// 拿到所有已经Ready的keys,通过keys可以拿到对应的channel
Set<SelectionKey> selectionKeys = selector.selectedKeys();
logger.debug("start iterator keys, size:{}", selectionKeys.size());
Iterator<SelectionKey> keyIterator = selectionKeys.iterator();
while (keyIterator.hasNext()) {
SelectionKey key = keyIterator.next();
if (key.isReadable()) {
logger.debug("key.isReadable(), startRead");
ByteBuffer input = ByteBuffer.allocate(64);
// 通过key.channel()可以拿到channel,需要进行下一步操作的话需要做一下强制转换
SocketChannel keyChannel = (SocketChannel) key.channel();
int readByte = keyChannel.read(input);
logger.debug("readByte " + readByte);
// 同样,read完成之后记得flip(),否则读不出数据
input.flip();
if (readByte == -1) {
logger.debug("readByte == -1, return!");
return;
}
for (int i = 0; i < readByte; i++) {
logger.debug("read [" + i + "]:" + input.get());
}
}
// 遍历结束之后记得删除key,否则key会一直存在于selector.selectedKeys()
keyIterator.remove();
}
break;
} else {
logger.debug("sleep(1000)");
Thread.sleep(1000);
}
}
}
}