• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question about NIO

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
reply
    Bookmark Topic Watch Topic
  • New Topic