File tree Expand file tree Collapse file tree 1 file changed +48
-0
lines changed
java-io/src/test/java/com/brianway/java/nio/tutorial Expand file tree Collapse file tree 1 file changed +48
-0
lines changed Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments