title: java.nio.channels.ClosedChannelException
body: <p>How can i solve this problem. I got following error:</p>
  
  <p><code>java.nio.channels.ClosedChannelException</code></p>
  
  <p>This is coding:</p>
  
  <pre><code> 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 &lt; i1 /* byteArray.length */; i++) {
                  String hex = Integer.toHexString(0xFF &amp; 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);
              }
          }
      }
  }
  </code></pre>