美文网首页
JavaNIO-通道10 SocketChannel 使用

JavaNIO-通道10 SocketChannel 使用

作者: 贪睡的企鹅 | 来源:发表于2019-07-25 20:29 被阅读0次

    SocketChannel

    用来表示Socket通道,由于是SelectableChannel子类,支持非阻塞和选择器。

    常用API

    isConnectionPending 判断此通道上是否正在进行连接操作。

        /**
         *
         *
         * 阻塞通道, IP 不存在
         */
        @Test
        public void test_isConnectionPending_connect() throws Exception {
            SocketChannel socketChannel = SocketChannel.open();
            long begin = 0;
            long end = 0;
            try {
                System.out.println(socketChannel.isConnectionPending());
                socketChannel.connect(new InetSocketAddress("192.168.0.123", 8888));
                socketChannel.isConnectionPending();
            } catch (Exception e) {
                System.out.println(socketChannel.isConnectionPending());
                e.printStackTrace();
            }
        }
    
        /**
         * 非阻塞通道, IP 不存在
         */
        @Test
        public void test_isConnectionPending_connect2() throws Exception {
            SocketChannel socketChannel = SocketChannel.open();
            long begin = 0;
            long end = 0;
            try {
                System.out.println(socketChannel.isConnectionPending());
                socketChannel.configureBlocking(false);
                socketChannel.connect(new InetSocketAddress("192.168.0.123", 8888));
                System.out.println(socketChannel.isConnectionPending());
            } catch (Exception e) {
                System.out.println(socketChannel.isConnectionPending());
                e.printStackTrace();
            }
        }
    
        /**
         * 阻塞通道, IP 存在
         */
        @Test
        public void test_isConnectionPending_connect3() throws Exception {
            SocketChannel socketChannel = SocketChannel.open();
            long begin = 0;
            long end = 0;
            try {
                System.out.println(socketChannel.isConnectionPending());
                socketChannel.configureBlocking(false);
                socketChannel.connect(new InetSocketAddress("localhost", 8888));
                System.out.println(socketChannel.isConnectionPending());
            } catch (Exception e) {
                System.out.println(socketChannel.isConnectionPending());
                e.printStackTrace();
            }
        }
    
        /**
         * 非阻塞通道, IP 存在
         */
        @Test
        public void test_isConnectionPending_connect4() throws Exception {
            SocketChannel socketChannel = SocketChannel.open();
            long begin = 0;
            long end = 0;
            try {
                System.out.println(socketChannel.isConnectionPending());
                socketChannel.configureBlocking(false);
                socketChannel.connect(new InetSocketAddress("localhost", 8888));
                System.out.println(socketChannel.isConnectionPending());
            } catch (Exception e) {
                System.out.println(socketChannel.isConnectionPending());
                e.printStackTrace();
            }
        }
    

    finishConnect 用来在连接失败后重新尝试连接

    @Test
        public void test_finishConnect_connect4() throws Exception {
            SocketChannel socketChannel = SocketChannel.open();
            long begin = 0;
            long end = 0;
            try {
                TimeUnit.SECONDS.sleep(1);
                socketChannel.configureBlocking(false);
                boolean result = socketChannel.connect(new InetSocketAddress("localhost", 8888));
                if(result==false){
                    while (!socketChannel.finishConnect()){
                        System.out.println("一直在尝试连接");
                        TimeUnit.SECONDS.sleep(1);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    

    open 同样用来连接服务端,其后设置setOption不会出现预期的效果

     @Test
        public void test_open_connect() throws Exception {
            SocketChannel socketChannel = SocketChannel.open();
            long begin = 0;
            long end = 0;
            try {
                begin = System.currentTimeMillis();
                socketChannel.configureBlocking(false);
                socketChannel.setOption(StandardSocketOptions.SO_RCVBUF,1234);
                //SocketChannel 默认是阻塞模式
                //在发生错误或连接到目标之前, connect ()方法一直是阻塞的
                socketChannel.open(new InetSocketAddress("localhost", 8888));
                end = System.currentTimeMillis();
                System.out.println(end - begin);
            } catch (Exception e) {
                end = System.currentTimeMillis();
                System.out.println(end - begin);
                e.printStackTrace();
            }
        }
    

    相关文章

      网友评论

          本文标题:JavaNIO-通道10 SocketChannel 使用

          本文链接:https://www.haomeiwen.com/subject/wemkrctx.html