Skip to content

Commit 77b4d8b

Browse files
committed
[add] add SocketChannelTest for NIO
1 parent 61d3d0f commit 77b4d8b

1 file changed

Lines changed: 37 additions & 0 deletions

File tree

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
}

0 commit comments

Comments
 (0)