Skip to content

Commit 2b21f5e

Browse files
committed
[add] add ServerSocketChannelTest for NIO
1 parent 77b4d8b commit 2b21f5e

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.brianway.java.nio.tutorial;
2+
3+
import org.junit.Test;
4+
5+
import java.io.IOException;
6+
import java.net.InetSocketAddress;
7+
import java.nio.channels.ServerSocketChannel;
8+
import java.nio.channels.SocketChannel;
9+
10+
/**
11+
* @auther brian
12+
* @since 2019/6/25 00:31
13+
*/
14+
public class ServerSocketChannelTest {
15+
16+
@Test
17+
public void testOpen() throws IOException {
18+
ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
19+
serverSocketChannel.socket().bind(new InetSocketAddress(9999));
20+
while (true) {
21+
System.out.println("waiting...");
22+
SocketChannel socketChannel =
23+
serverSocketChannel.accept();
24+
//do something with socketChannel...
25+
System.out.println("connected: " + socketChannel.toString());
26+
}
27+
}
28+
29+
@Test
30+
public void testNonBlocking() throws IOException {
31+
ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
32+
33+
serverSocketChannel.socket().bind(new InetSocketAddress(9999));
34+
serverSocketChannel.configureBlocking(false);
35+
36+
while (true) {
37+
System.out.println("waiting...");
38+
SocketChannel socketChannel =
39+
serverSocketChannel.accept();
40+
41+
if (socketChannel != null) {
42+
//do something with socketChannel...
43+
System.out.println("connected: " + socketChannel.toString());
44+
}
45+
}
46+
}
47+
48+
}

0 commit comments

Comments
 (0)