Jump to content
  • Advertisement
Sign in to follow this  
bilsa

[java] ClosedSelectorException with NIO?

This topic is 4591 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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!