SocketChannel doesn't wait

Hi there,

the Socket Channel doesn’t wait for incoming messages. The select() Method always return.

–> I got 100% CPU utilization because the while is always running.

Any suggestions ?

Here is the code snippet:

InetSocketAddress socket = new InetSocketAddress(“C9769CE0”,8100);
socketChannel = SocketChannel.open();
socketChannel.configureBlocking(false);
socketChannel.connect(socket);
socketChannel.finishConnect();

        Selector selector = SelectorProvider.provider().openSelector();            
        SelectionKey key = socketChannel.register(selector,SelectionKey.OP_READ);
        CharBuffer charBuffer = CharBuffer.allocate(1024);
        b.clear();      
        int keysAdded;
        while (true) 
        {
          key.selector().select();
                              ................

–> the select method dont wait here for incoming messages ?

You might have an error condition waiting on the socket (like “Could not connect”). Have you checked that?

  • elias

socketChannel.configureBlocking(false);

That would be the problem, wouldn’t it?

[quote]socketChannel.configureBlocking(false);

That would be the problem, wouldn’t it?
[/quote]
I would be surprised…

Selector.select() does block, indefinitely, according to the NIO specification.

From the API docs, a READ selector ought to block until one of it’s channels has received incoming data which your code has not yet read out, OR until any of its channels has been disconnected (not in the docs :().

There are other alternatives, but these are all official bugs with e.g. 1.4.0 and 1.4.1 on specific platforms, and have IIRC all been fixed.

Hi,

there is a bug in the bug database from sun. It seems that I have this problem:

http://developer.java.sun.com/developer/bugParade/bugs/4850373.html

Raven

Update: It seem to be fixed in jdk1.5.0 beta2, according to the bug status.

  • elias