Jump to content
  • Advertisement
Sign in to follow this  
lazyworm

Proper way to close a Java SocketChannel

This topic is 2785 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

I'm writing a game server using Java non-blocking SocketChannel. Sometimes I want to send a reply message and then close the channel, like the code below.

However, the close() method interrupts the write() method, I get a java.nio.channels.ClosedChannelException and the message is not sent.

I can pop a thread and wait for 1-2 seconds before closing the channel, but I feel it's so wasteful to making another thread.

What is the proper way to close a SocketChannel while there are pending operations?

code:

String msg = "Wrong password!";
channel.write(ByteBuffer.wrap(msg.getBytes()));
channel.close();

Share this post


Link to post
Share on other sites
Advertisement
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!