If we are creating a online-chat sever, i think we should only need a single ServerSocketChannel, isn’t it ?
If we get only one ServerSocketChannel, why we need a Selector ?
Could anyone gives me a real project example needs to use Selector ?
Thanks !
When you
accept()on aServerSocketChannel, what you get back is a newSocketChannelwhich you thenread()andwrite()to communicate to that client.Your selection code would typically
select(), then iterate through the selected keys, accepting any key thatisAcceptable()(thus opening a new channel), but then also reading and writing any key (presumably one of your accepted client channels) thatisReadable()orisWritable().