java.nio.channels.ClosedChannelException

How can i solve this problem. I got following error:

java.nio.channels.ClosedChannelException

This is coding:

 public void run() { try { SocketChannel socketChannel = (SocketChannel) key.channel(); ByteBuffer buffer = ByteBuffer.allocate(512); int i1 = socketChannel.read(buffer); if (buffer.limit() == 0 || i1 == -1) { Socket s = null; try { s = socketChannel.socket(); s.close(); key.cancel(); } catch (IOException ie) { if (UnitDataServer.isLog) { log.error("Error closing socket " + s + ": " + ie); } } } else { buffer.flip(); if (UnitDataServer.isLog) { log.info(" Recvd Message from Unit : " + buffer.array()); } byte byteArray[] = buffer.array(); log.info("Byte Array length :" + byteArray.length); hexString = new StringBuffer(); for (int i = 0; i < i1 /* byteArray.length */; i++) { String hex = Integer.toHexString(0xFF & byteArray[i]); if (hex.length() == 1) { // could use a for loop, but we're only dealing with a // single byte hexString.append('0'); } hexString.append(hex); } hexString.trimToSize(); log.info("Hex String :" + hexString); Communicator.dataReceive(new DataReceive( socketChannel, hexString.toString(), dst)); } } catch (Exception e) { if (UnitDataServer.isLog) { // log.error(e); } try { socketChannel.socket().close(); key.cancel(); } catch (IOException ex) { if (UnitDataServer.isLog) { log.error(ex); } } }
}
2

2 Answers

You have closed the channel and are still trying to use it.

There are several issues with your code.

First, your test for EOS is faulty. Remove the limit() == 0 test. That doesn't indicate EOS, it just indicates a zero length read, which can happen in non-blocking mode at any time. It doesn't mean the peer has closed his end of the connection, and it doesn't mean you should close your end.

Second, closing a channel closes the socket as well. You should close the channel only, not the socket.

Third, closing a channel cancels the key. You don't need to follow every close with a cancel.

You may also have failed to check whether a ready key is valid in the select loop before using it, e.g. for reading.

I continue to be amazed, and amused, and bemused, by the claim elsewhere in this thread that 'source code is untrue' under some circumstances.

0

You need to fix/secure code that is throwing this exception. ClosedChannelException is ...

... thrown when an attempt is made to invoke or complete an I/O operation upon channel that is closed, or at least closed to that operation. That this exception is thrown does not necessarily imply that the channel is completely closed. A socket channel whose write half has been shut down, for example, may still be open for reading

(as described in Java 6 API)

But really, you would need to provide us code snipped and stack trace in order to get more detailed help.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like