Sign in to follow this  

[java] ClosedSelectorException with NIO?

This topic is 4380 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi! merry christmas to you all! Im having a problem with NIO. I get the ClosedSelectorException exception when i call selector.select(); This happens only, and immediately after I try to manipulate the byte[] data that I send. for eg:

selector.select();
...
if(key.isWriteable()) 
{
   SendHandler sendHandler = (SendHandler)key.attachment();
   sendHandler.send();
}
...

SendHandler.Send()
{
   byte[] dataToSend = queue.getNextSendData();
   ByteBuffer outBuffer = ByteBuffer.wrap(dataToSend);
   ... 
   //sends the data
   ...
   
   //notifies MyClass.onPacketSent.onPacketSent(...)
   notifyOnPacketSentListeners(dataToSend);
}


class MyClass
{
  Encryption myEncryption = new Encryption();

  IPacketSentListener onPacketSent = new IPacketSentListener()
  {
    public void onPacketSent(byte[] sentData)
    {
      //Now I want to decrypt the sentData[] since it was sent as
      //encrypted data... so we recieved encrypted data
      
      //So we decrypt the data
      //As soon as I add this line of code... the next frame
      //the selector will be closed!
      //This function is called FROM the selector's THREAD
      //BUT the myEncryption object has been created by
      //another thread!

      //CAUSES THE SELECTOR TO CLOSE:
      myEncryption.decrypt(sentData);

      //If I do like this instead... it works just fine:
      //Encryption myEncryption = new Encryption();
      //myEncryptio.decrypt(sentData);

      //Printing the data works just fine... if I don't use
      //the myEncryption.decrypt(sentData);
      //And we print it to the console
      PrintPacketData(sentData);
    }
  };
}


Ok... now I have located the problem! I'm no expert on threads... but I would BET my ass that this problem somehow is caused by the fact that the myEncryption object is created and owned by another thread... And then the selector's thread sends the packet data and calls on the function that uses this object... so the object is used in the selector's thread ?? Would appreciate if someone could help me out on this! [Edited by - bilsa on December 19, 2005 4:42:43 PM]

Share this post


Link to post
Share on other sites
Ohhhh my god! :)

myEncryption == null ehe;)

I just couldn't believe that it didn't get initialized ??!
Seems like it was some synchronization problem.

Thx anyway guys!

Share this post


Link to post
Share on other sites

This topic is 4380 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this