4 Replies Latest reply on Oct 14, 2011 9:13 AM by 846950

    SocketChannel.register blocks forever


      I have a NIO client that should connect to multiple servers.
      The first connection is ok, works fine.

      But later on, I try to connect to a second server, and I can't register the new SocketChannel, it freezes on this line:
      socketChannel.register(this.selector, SelectionKey.OP_CONNECT);

      Here is the code of the full method:

      protected SocketChannel connectToServer(String id, InetAddress inetAddress, int port) throws IOException {
           SocketChannel socketChannel = SocketChannel.open();
           socketChannel.connect(new InetSocketAddress(inetAddress, port));

           logger.debug("Register socketChannel"); // <-- logs
           socketChannel.register(this.selector, SelectionKey.OP_CONNECT);
           logger.debug("SocketChannel registered"); // <-- doesn't get logged

           ServerInfo serverInfo = new ServerInfo();
           SocketChannelInfoClient s = new SocketChannelInfoClient();
           s.serverInfo = serverInfo;
           this.sockets.put(socketChannel, s);
           return socketChannel;

      Anyone has an idea why it blocks ?

      Thank you for any help!