java channels_java.nio.channels.ClosedChannelException

我怎么解决这个问题.我收到以下错误:

java.nio.channels.ClosedChannelException

这是编码:

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);

}

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值