标题:nio中设置非阻塞导致java.nio.channels.IllegalBlockingModeException
只看楼主
郭赛
Rank: 1
等 级:新手上路
帖 子:31
专家分:0
注 册:2017-12-9
结帖率:77.78%
 问题点数:0 回复次数:0 
nio中设置非阻塞导致java.nio.channels.IllegalBlockingModeException
程序代码:
public class non_blocking_nio {
    /**
     * 上传文件
     */

    @Test
    public void client(){
        SocketChannel socketchannel = null;
        FileChannel fchannel = null;
        try {
            socketchannel = SocketChannel.open(new InetSocketAddress("localhost",1001));
            socketchannel.configureBlocking(false);//设置成非阻塞
            //fchannel = FileChannel.open(Paths.get("C:\\Users\\郭赛\\Desktop\\angle王鸥\\鸥茉莉.jpg"), StandardOpenOption.READ);
            ByteBuffer buf = ByteBuffer.allocate(1024);
            buf.put("hello".getBytes());
            buf.flip();
            socketchannel.write(buf);
            buf.clear();
            
        } catch (IOException e) {
            e.printStackTrace();
        }finally {

            if(socketchannel == null) {
                try {
                    socketchannel.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    @Test
    public void server(){
        //SocketChannel schannel = null;
        Iterator<SelectionKey> it = null;
        try {
            ServerSocketChannel ssc = ServerSocketChannel.open();
            ssc.configureBlocking(false);
            ssc.bind(new InetSocketAddress(1001));
            //SocketChannel socketchannel = ssc.accept();
            //FileChannel fchannel = FileChannel.open(Paths.get("C:\\Users\\郭赛\\Desktop\\鸥茉莉001.jpg"),StandardOpenOption.WRITE,StandardOpenOption.CREATE);
            Selector selector = Selector.open();
            //设置监听
            //int opAccept = SelectionKey.OP_ACCEPT;
             ssc.register(selector,SelectionKey.OP_ACCEPT);
            //开启轮询
            while(selector.select() > 0){
                Set<SelectionKey> selectionKeys = selector.selectedKeys();//获取当前所有监听事件,每一次都不同
                //取出每一个监听事件,因为在监听的过程中可能会有其他事件来所以采用迭代的方法
                it = selectionKeys.iterator();
                while(it.hasNext()){
                    SelectionKey skey = it.next();//获取到监听事件
                    if(skey.isAcceptable()){
                        //获取socketchannel,连接和客户端通道
                        SocketChannel schannel = ssc.accept();
                        //设置为非阻塞
                        schannel.configureBlocking(false);
                        //修改监听模式为读就绪
                        schannel.register(selector,SelectionKey.OP_READ);

                    }
                    else if(skey.isReadable()){
                        //获取channel
                        SocketChannel schannel = (SocketChannel) skey.channel();
                        ByteBuffer buf = ByteBuffer.allocate(1024);
                        //开始通信
                        int len;

                        while((len = schannel.read(buf)) > 0){
                            buf.flip();
                            System.out.println(new String(buf.array(),0,len));
                            //System.out.println(schannel.read(buf)
                            //初始化,能放更多数据
                            buf.clear();
                        }
                        //schannel.write("上传完成".getBytes());
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            it.remove();
        }

    }
}

哪位大哥能不能运行一下我这个程序,一直提示    java.nio.channels.IllegalBlockingModeException但是找不到原因

[此贴子已经被作者于2020-6-4 22:11编辑过]

搜索更多相关主题的帖子: 监听 事件 open 非阻塞 null 
2020-06-04 22:08



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-501970-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.026432 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved