NIO的简单使用

目的是启动一个服务端等待客户端链接,链接成功后,客户端发送字符串,服务端接收后展现。
服务端:java

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.Set;

public class NIOServer {
    public static void main(String[] args) throws IOException {
        ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
        Selector selector = Selector.open();
        serverSocketChannel.socket().bind(new InetSocketAddress(6666));
        serverSocketChannel.configureBlocking(false);
        serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT); //ServerSocketChannel注册到Selector,监听事件为链接
        while(true){

            if(selector.select(1000) == 0){
                //阻塞1s没有事件发生,不须要等待,能够作其余工做
                System.out.println("服务器等待1s,无链接");
                continue;
            }

            Set<SelectionKey> selectionKeys = selector.selectedKeys(); //获取到有链接事件发生的channel的selectionKeys集合

            Iterator<SelectionKey> iterator = selectionKeys.iterator();
            while (iterator.hasNext()){

                SelectionKey key = iterator.next();

                if(key.isAcceptable()){
                    //链接事件
                    SocketChannel socketChannel = serverSocketChannel.accept(); //accept()不会阻塞
                    socketChannel.configureBlocking(false);
                    socketChannel.register(selector,SelectionKey.OP_READ, ByteBuffer.allocate(1024));
                    System.out.println("客户单链接成功,生成一个socketchannel:" + socketChannel.hashCode());
                }

                if(key.isReadable()){
                    //读事件
                    SocketChannel socketChannel = (SocketChannel) key.channel();
                    ByteBuffer byteBuffer = (ByteBuffer) key.attachment();
                    socketChannel.read(byteBuffer);
                    System.out.println("form客户端数据:" + new String(byteBuffer.array()));
                }
                iterator.remove();
            }

        }
    }
}

客户端:服务器

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

public class NIOClient {
    public static void main(String[] args) throws IOException {

        SocketChannel socketChannel = SocketChannel.open();
        socketChannel.configureBlocking(false);
        InetSocketAddress inetSocketAddress = new InetSocketAddress("127.0.0.1", 6666);

        if(!socketChannel.connect(inetSocketAddress)){
            while (!socketChannel.finishConnect()){
                System.out.println("链接须要时间,客户端不会阻塞,能够作其余工做");
            }
        }

        //链接成功
        String str = "hello NIOServer";

        ByteBuffer byteBuffer = ByteBuffer.wrap(str.getBytes());

        socketChannel.write(byteBuffer);

        System.in.read();

    }
}
相关文章
相关标签/搜索