I have written a simple java nio program like the below
public static void main(String[] args) throws IOException, InterruptedException {
InetSocketAddress address = new InetSocketAddress("127.0.0.1",1001);
Selector incomingMessageSelector = Selector.open();
SocketChannel socketChannel = SocketChannel.open();
socketChannel.configureBlocking(false);
// Till here the code creates the top 2 connections to port 52209 and 52210
socketChannel.connect(address);
socketChannel.register(incomingMessageSelector, SelectionKey.OP_CONNECT);
socketChannel.register(incomingMessageSelector, SelectionKey.OP_WRITE);
socketChannel.register(incomingMessageSelector, SelectionKey.OP_READ);
// Then it creates 2 connections to port 1001
Thread.sleep(900000L);
}
I want to understand why it creates 4 connections, with the standard TCP blocking library it tends to create 2 connections.
I use JDK 1.7 and Windows 7.
In the image only the 4 highlighted connections are of interest which are created by the client.
One connection 1 entry marked with red is the server port.
PFA a image which shows these 4 connections.!
Well i acutally most puzzled about why
Selector incomingMessageSelector = Selector.open();
creates a connection on an dynamic port
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…