Amith
Amith

Reputation: 1967

when a SelectionKey turns writable in java nio

Now my server reads all the message from client. Now it is a one way communication. I want to send message to client by the server. Now server reads message when the key is READABLE STATE it didnt turn to writable state i just want to know when the key changes to writable.

Upvotes: 1

Views: 3336

Answers (2)

user207421
user207421

Reputation: 311023

i just want to know when the key changes to writable

It is almost always writable. OP_WRITE is triggered whenever there is free space in the socket send buffer, which is almost all the time. Just call write(), and if you get a zero return then you start getting interested in OP_WRITE: you use it to tell you when the channel has become writable again.

But you don't need to wait for this event, unless you have just had a zero-length return from write(). In the normal case you should just write when ready, and only have OP_WRITE registered in the intervals between when write() returns zero and when it doesn't after you retry having registered OP_WRITE and had it fire.

Upvotes: 5

Amith
Amith

Reputation: 1967

key.interestOps(SelectionKey.OP_WRITE);

Upvotes: -1

Related Questions