File tree Expand file tree Collapse file tree
java-io/src/test/java/com/brianway/java/nio/tutorial Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package com .brianway .java .nio .tutorial ;
2+
3+ import org .junit .Assert ;
4+ import org .junit .Test ;
5+
6+ import java .io .IOException ;
7+ import java .net .InetSocketAddress ;
8+ import java .nio .channels .SocketChannel ;
9+
10+ /**
11+ * @auther brian
12+ * @since 2019/6/24 23:33
13+ */
14+ public class SocketChannelTest {
15+
16+ @ Test
17+ public void testConnect () throws IOException {
18+ SocketChannel socketChannel = SocketChannel .open ();
19+ boolean connected = socketChannel .connect (new InetSocketAddress ("www.baidu.com" , 80 ));
20+ Assert .assertEquals (true , connected );
21+ socketChannel .close ();
22+ }
23+
24+ @ Test
25+ public void testNonBlocking () throws IOException {
26+ SocketChannel socketChannel = SocketChannel .open ();
27+ socketChannel .configureBlocking (false );
28+ boolean connected = socketChannel .connect (new InetSocketAddress ("www.baidu.com" , 80 ));
29+ System .out .println ("connected: " + connected );
30+ while (!socketChannel .finishConnect ()) {
31+ //wait, or do something else...
32+ System .out .println ("not finish" );
33+ }
34+ System .out .println ("finished" );
35+ }
36+
37+ }
You can’t perform that action at this time.
0 commit comments