henryevol
发贴: 18
积分: 12
|
于 2005-06-04 16:05
对于阻塞式的Channel连接, socketChannel.write(buff); 直到写完了,才会返回 而对于非阻塞式的连接 可能写完了返回,也可以写了一点之后就有返回 返回值为 已发送的数值 下面函数是对非阻塞通道进行的阻塞式调用! 可以参考
public static void channelWrite(SocketChannel channel, ByteBuffer writeBuffer, long sleepInterval) throws IOException { long nbytes = 0; long toWrite = writeBuffer.remaining(); try { while (nbytes != toWrite) { nbytes += channel.write(writeBuffer); try { Thread.sleep(sleepInterval); } catch (InterruptedException e) {} } } finally { writeBuffer.rewind(); } }
|