This discussion is archived
4 Replies Latest reply: Oct 14, 2011 2:13 AM by 846950 RSS

SocketChannel.register blocks forever

846950 Newbie
Currently Being Moderated
Hi,

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.configureBlocking(false);
     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();
     serverInfo.setId(id);
     serverInfo.setIp(inetAddress.getHostAddress());
     serverInfo.setPort(port);
     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!

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points