views:

38

answers:

0

I have a problem where I am able to read from any connection only once and then never again.

When I select I do basically the following in a while(true) loop:

if(selector.select( 500 ) == 0) 
     continue;

for(SelectionKey sk : selector.keys()) {
     // if (sk != anyOtherSk()) { obj = createAnObject(sk); }
     //  obj.readChannel();
}

Then, in the object's code I do the following:

public void readChannel() {
    BufferedReader reader = new BufferedReader(Channels.newReader((ReadableByteChannel) sk.channel(), "US-ASCII"));
    doSomething(reader.readLine());
    Writer out = Channels.newWriter((WritableByteChannel) key.channel(), "US-ASCII");
    out.write("something"); 
    sk.channel().register(sk.selector(), SelectionKey.OP_READ);
}

At this point the object's code finishes executing (as I've confirmed), but any further messages from the client aren't ever acted upon or even, it appears, recieved.

if(selector.select( 500 ) == 0)

continually fails to detect messages, even though I know they have been sent.