为了账号安全,请及时绑定邮箱和手机立即绑定

Java NIO写入异常问题

Java NIO写入异常问题

ITMISS 2019-03-16 15:38:48
在使用NIO进行写入数据时,我把缓冲区增大1000k,为什么这时会出现没有写完的情况??public class Server {  public static void main(String[] args) throws Exception {     ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();     serverSocketChannel.configureBlocking(false);     serverSocketChannel.bind(new InetSocketAddress("localhost", 7077));     Selector selector = Selector.open();     serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);     selector.select();     Set<SelectionKey> selectedKeysSet = selector.selectedKeys();     Iterator<SelectionKey> iterator = selectedKeysSet.iterator();    while (iterator.hasNext()) {       SelectionKey key = iterator.next();       SocketChannel socketChannel = ((ServerSocketChannel) key.channel()).accept();       socketChannel.configureBlocking(false);       ByteBuffer byteBuffer = ByteBuffer.allocate(1000 * 1024);       System.out.println("byteBuffer.limit()=" + byteBuffer.limit());       socketChannel.write(byteBuffer);       System.out.println("byteBuffer.position()=" + byteBuffer.position());       socketChannel.close();     }     serverSocketChannel.close();   } }public class Client {  public static void main(String[] args) throws Exception {     SocketChannel channel1 = SocketChannel.open();     channel1.connect(new InetSocketAddress("localhost", 7077));     TimeUnit.MINUTES.sleep(1);     channel1.close();   } }byteBuffer.limit()=1024000byteBuffer.position()=261676
查看完整描述

2 回答

?
烙印99

TA贡献1829条经验 获得超13个赞

没有规定必须一次就把全部数据写完吧,

查看完整回答
反对 回复 2019-03-16
  • 2 回答
  • 0 关注
  • 570 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号