Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
508 views
Welcome To Ask or Share your Answers For Others

1 Answer

The image is very small but on closer investigation you have

  • two Java processes
  • the first process has a connection to itself. There is a connection for each end, port 52209 and 52210.
  • it also has a connection from the second process on port 1001.
  • the second process is the client you are running with one connection to port 1001

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...