码迷,mamicode.com
首页 > 编程语言 > 详细

JAVA nio 简单使用

时间:2019-10-06 00:32:52      阅读:80      评论:0      收藏:0      [点我收藏+]

标签:row   serve   服务   shc   cal   todo   and   interrupt   nts   

nio 模拟客户端和服务器互相通讯--传输一个int值,并且不断的+1;
服务器

public class Server {
    public static void main(String[] args) {
        try {
            ServerSocketChannel server=ServerSocketChannel.open().bind(new InetSocketAddress(8881));
            server.configureBlocking(false);
            Selector selector=Selector.open();
            server.register(selector,SelectionKey.OP_ACCEPT);
            for(;;) {
                selector.select();
                Set<SelectionKey> keys = selector.selectedKeys();
                Iterator<SelectionKey> iterator = keys.iterator();
                while(iterator.hasNext()) {
                    SelectionKey next = iterator.next();
                    if(next.isAcceptable()) {
                        acceptHandle(next, selector);
                    }
                    if (next.isReadable()) {
                        doRead(next, selector);
                    }
                    iterator.remove();
                }
                Thread.sleep(2000);
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    public static void acceptHandle(SelectionKey key,Selector selector) throws IOException {
        ServerSocketChannel serverShannel =(ServerSocketChannel) key.channel();
        SocketChannel channel = serverShannel.accept();
        channel.configureBlocking(false);
        channel.register(selector, SelectionKey.OP_READ,ByteBuffer.allocate(1024));
    }
    public static  void doRead(SelectionKey key,Selector selector) throws IOException {
        SocketChannel socketChannel = (SocketChannel) key.channel();
        ByteBuffer buffer =(ByteBuffer) key.attachment();
        buffer.clear();
        int read = socketChannel.read(buffer);
        int msg=buffer.getInt(0);
        System.out.println("服务器收到客户端"+socketChannel.getLocalAddress()+"  "+msg);
        buffer.rewind();
        buffer.putInt(msg+1);
        buffer.flip();
        socketChannel.write(buffer);
        //buffer.clear();
    }
}

客户端

public class Client {
    public static void main(String[] args) {
        try(SocketChannel channel=SocketChannel.open();
            Selector selector=Selector.open();
                ) {
            channel.configureBlocking(false);
            if(!channel.connect(new InetSocketAddress("127.0.0.1", 8881))) {
                while(!channel.finishConnect()) {};
                System.out.println("连接到服务器");
            }
            
            ByteBuffer buffer = ByteBuffer.allocate(1024);
            buffer.putInt(1);
            buffer.flip();
            channel.write(buffer);
            channel.register(selector, SelectionKey.OP_READ,buffer);
            for(;;) {
                selector.select();
                Set<SelectionKey> keys = selector.selectedKeys();
                Iterator<SelectionKey> iterator = keys.iterator();
                while(iterator.hasNext()) {
                    SelectionKey next = iterator.next();
                    if(next.isReadable()) {
                        doRead(next,selector);
                    }
                    iterator.remove();
                }
                Thread.sleep(2000);
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    public static void doRead(SelectionKey key,Selector selector) throws IOException {
        SocketChannel socketChannel = (SocketChannel) key.channel();
        ByteBuffer buffer =(ByteBuffer) key.attachment();
        buffer.clear();
        socketChannel.read(buffer);
        int msg=buffer.getInt(0);
        System.out.println("客户端收到服务器返回的信息"+socketChannel.getLocalAddress()+"  "+msg);
        buffer.putInt(0,msg+1);
        buffer.flip();
        socketChannel.write(buffer);
        
    }
    
}

JAVA nio 简单使用

标签:row   serve   服务   shc   cal   todo   and   interrupt   nts   

原文地址:https://www.cnblogs.com/duangL/p/11625953.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!