• Post Reply Bookmark Topic Watch Topic
  • New Topic

Question about NIO  RSS feed

 
will wang
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi..guys,i encountered some problems with NIO,the situation is :
I implemented a server with NIO,and implemented a client with the traditional blocking socket.
Firstly, i registered the client socket channel with the OP_READ and OP_WRITE operations when the serve accept the connection,the code is :
//////////////////////////////////////////
if (key.isAcceptable()){
ServerSocketChannel server =(ServerSocketChannel) key.channel();
SocketChannel clientChannel = server.accept();
clientChannel.configureBlocking(false);
clientChannel.register(this.selector,SelectionKey.OP_READ|SelectionKey.OP_WRITE);
////////////////////////////////////////
when the client is running, it will sending a byte array to the serve per second,but i found that the server just knew the accpet and write event,in other words,no READ key will be selected, i feel confused about this...
 
Jon Dornback
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try this: put a print statement in the loop where you detect which operation is ready, and print out the ready operations. i think what you're describing is similar to a problem i had - the socket is always ready to write (unless it is being written to), so it always notifies the selector and sends your code in to a constant loop instead. to fix it, i simply didn't register the socket for write operations. this doesn't prevent you from actually writing to the socket, but it won't notify the selector all the time. also, after a socket is connected, you may want to de-register the accept operation because sometimes that can cause problems as well, in that even though it is already connected, it for some reason still thinks it can be connected.
hope that wasn't too confusing.
Jon
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!