BIO,NIO,AIO编程

传统BIO编程

服务端:

public class TimeServer {
    public static void main(String[] args) throws IOException {
        int port=8000;

        if (args!=null && args.length>0){
            try {
                port=Integer.parseInt(args[0]);
            }catch (NumberFormatException e){

            }
        }

        ServerSocket serverSocket=null;
        try {
            serverSocket=new ServerSocket(port);
            System.out.println("time server 启动端口:"+port);

            Socket socket=null;
            while (true){
                socket=serverSocket.accept();
                new Thread(new TimeServerHandler(socket)).start();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (serverSocket!=null){
                System.out.println("time server 关闭");
                serverSocket.close();
                serverSocket=null;
            }
        }
    }
}
public class TimeServerHandler implements Runnable {
    private Socket socket;
    public TimeServerHandler(Socket socket) {
        this.socket=socket;
    }

    @Override
    public void run() {
        BufferedReader in=null;
        PrintWriter out=null;

        try {
            in=new BufferedReader(new InputStreamReader(this.socket.getInputStream()));

            out=new PrintWriter(this.socket.getOutputStream(),true);
            String currentTime=null;
            String body=null;
            while (true){
                body=in.readLine();
                if (body==null){
                    break;
                }
                System.out.println("time server 接收顺序:"+body);
                currentTime ="QUERY TIME ORDER".equalsIgnoreCase(body)?new Date(System.currentTimeMillis()).toString():"BAD ORDER";
                out.println(currentTime);
            }
        } catch (IOException e) {
            if (in!=null){
                try {
                    in.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
            if (out!=null){
                out.close();
                out=null;
            }
            if (this.socket!=null){
                try {
                    this.socket.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
            this.socket=null;
        }
    }
}

客户端:

public class TimeClient {
    public static void main(String[] args) {
        int port=8000;

        if (args!=null && args.length>0){
            try {
                port=Integer.parseInt(args[0]);
            }catch (NumberFormatException e){

            }
        }
        Socket socket=null;
        BufferedReader in=null;
        PrintWriter pw=null;

        try {
            socket = new Socket("127.0.0.1",port);
            in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            pw = new PrintWriter(socket.getOutputStream(),true);

            pw.println("QUERY TIME ORDER");
            System.out.println("send order to server succed.");
            String resp = in.readLine();
            System.out.println("现在是:"+resp);
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (pw!=null){
                pw.close();
            }
            if (in!=null){
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (socket!=null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            socket=null;
        }
    }
}


伪异步IO

服务端:

public class TimeServer {
    public static void main(String[] args) {
        int port=8000;

        if (args!=null && args.length>0){
            try {
                port=Integer.parseInt(args[0]);
            }catch (NumberFormatException e){

            }
        }

        ServerSocket serverSocket = null;
        try {
            serverSocket = new ServerSocket(port);
            System.out.println("time server 启动,端口:" + port);

            Socket socket = null;
            while (true) {
                socket = serverSocket.accept();
                TimeserverHandlerExecutePool sigleExecutor= new TimeserverHandlerExecutePool(50,10000);
                sigleExecutor.execute(new TimeServerHandler(socket));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
public class TimeserverHandlerExecutePool{
    private  ExecutorService executor;
    public TimeserverHandlerExecutePool(int maxPoolSize, int queueSize) {
        executor = new ThreadPoolExecutor(Runtime.getRuntime().availableProcessors(),
                maxPoolSize,120L, TimeUnit.SECONDS,
                new ArrayBlockingQueue<Runnable>(queueSize));
    }

    public void execute(Runnable task){
        executor.execute(task);
    }
}

客户端随意


NIO编程

服务端:

public class TimeserverHandlerExecutePool{
    private  ExecutorService executor;
    public TimeserverHandlerExecutePool(int maxPoolSize, int queueSize) {
        executor = new ThreadPoolExecutor(Runtime.getRuntime().availableProcessors(),
                maxPoolSize,120L, TimeUnit.SECONDS,
                new ArrayBlockingQueue<Runnable>(queueSize));
    }

    public void execute(Runnable task){
        executor.execute(task);
    }
}

public class MultiplexerTimeServer implements Runnable {
    private Selector selector;
    private ServerSocketChannel acceptorSvr;
    private volatile boolean stop;

    public  MultiplexerTimeServer (int port){
        try {
            acceptorSvr = ServerSocketChannel.open();
            //绑定端口
            acceptorSvr.socket().bind(new InetSocketAddress(port));
            //设置非阻塞
            acceptorSvr.configureBlocking(false);

            selector=Selector.open();
            acceptorSvr.register(selector,SelectionKey.OP_ACCEPT);
            System.out.println("time server 在端口:"+port+" 上启动");
        } catch (IOException e) {
            System.exit(1);
        }
    }
    @Override
    public void run() {
        while (!stop){
            try {
                selector.select(1000);
                Set<SelectionKey> selectionKeys = selector.selectedKeys();
                Iterator<SelectionKey> iterator=selectionKeys.iterator();
                SelectionKey selectionKey;

                while (iterator.hasNext()){
                    selectionKey =iterator.next();
                    iterator.remove();
                    try {
                        handleInput(selectionKey);
                    }catch (Exception e){
                        if (selectionKey!=null){
                            selectionKey.cancel();
                            if (selectionKey.channel()!=null){
                                selectionKey.channel().close();
                            }
                        }
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (selector!=null){//关闭多路复用器,channel会自动被关闭
            try {
                selector.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private void handleInput(SelectionKey selectionKey) throws IOException {
        if (selectionKey.isValid()){
            if (selectionKey.isAcceptable()){
                ServerSocketChannel ssc= (ServerSocketChannel) selectionKey.channel();

                SocketChannel sc=ssc.accept();
                sc.configureBlocking(false);

                sc.register(selector,SelectionKey.OP_READ);

            }
            if (selectionKey.isReadable()){
                SocketChannel sc= (SocketChannel) selectionKey.channel();
                ByteBuffer readBuffer = ByteBuffer.allocate(1024);

                int readBytes=sc.read(readBuffer);
                if (readBytes>0){
                    readBuffer.flip();
                    byte[] bytes = new byte[readBuffer.remaining()];
                    readBuffer.get(bytes);

                    String body=new String(bytes,"utf-8");
                    String currentTime =new Date(System.currentTimeMillis()).toString();
                    doWrite(sc,currentTime+"\r\n");
                } else if (readBytes < 0){
                    //对端链路关闭
                    selectionKey.cancel();
                    sc.close();
                }else {
                    //读到0字节,忽略
                }
            }
        }
    }

    private void doWrite(SocketChannel sc, String currentTime) throws IOException {
        if (currentTime!=null && currentTime.trim().length()>0){
            byte[] bs = currentTime.getBytes();
            ByteBuffer wb=ByteBuffer.allocate(bs.length);
            wb.put(bs);
            wb.flip();
            sc.write(wb);
        }
    }
}

客户端:

public class TimeClientHandler implements Runnable {
    private String host;
    private int port;
    private Selector selector;
    private SocketChannel socketChannel;
    private volatile boolean stop;

    public static void main(String[] args) {
        Thread t= new Thread(new TimeClientHandler(null,8000));
        t.setDaemon(false);
        t.start();
    }
    public TimeClientHandler(String host,int port){
        this.host=host==null?"127.0.0.1":host;
        this.port=port;

        try {
            selector =Selector.open();
            socketChannel =SocketChannel.open();
            socketChannel.configureBlocking(false);
        } catch (IOException e) {
            e.printStackTrace();
            System.exit(1);
        }
    }
    @Override
    public void run() {
        try {
            doConnect();
        } catch (IOException e) {
            e.printStackTrace();
            System.exit(1);
        }
        while (!stop){
            try {
                selector.select(1000);

                Set<SelectionKey> selectionKeys=selector.selectedKeys();

                Iterator<SelectionKey> iterator=selectionKeys.iterator();

                while (iterator.hasNext()){
                    SelectionKey selectionKey=iterator.next();
                    iterator.remove();
                    try {
                        handleInput(selectionKey);
                    }catch (Exception e){
                        selectionKey.cancel();
                        if (selectionKey.channel()!=null)
                            selectionKey.channel().close();
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
                System.exit(1);
            }
        }
        if (selector!=null){
            try {
                selector.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private void handleInput(SelectionKey selectionKey) throws IOException {
        if (selectionKey.isValid()){
            SocketChannel sc= (SocketChannel) selectionKey.channel();
            if (selectionKey.isConnectable()){
                if(sc.finishConnect()) {
                    sc.register(selector, SelectionKey.OP_READ);
                    doWrite(sc);
                }else {
                    System.exit(1);
                }
            }
            if (selectionKey.isReadable()){
                ByteBuffer readBuf=ByteBuffer.allocate(1024);
                int readBytes = sc.read(readBuf);
                if (readBytes>0){
                    readBuf.flip();
                    byte[]  bytes=new byte[readBuf.remaining()];
                    readBuf.get(bytes);
                    String body=new String(bytes,"utf-8");
                    System.out.println("now is:"+body);
                    this.stop=true;
                }else if (readBytes<0){
                    selectionKey.cancel();
                    sc.close();
                }else {

                }
            }
        }
    }

    private void doConnect() throws IOException{
       //如果直接连接成功,则注册到多路复用器上,发送请求消息,读应答
        if (socketChannel.connect(new InetSocketAddress(host,port))){
            socketChannel.register(selector, SelectionKey.OP_READ);
            doWrite(socketChannel);
        }else {
            socketChannel.register(selector,SelectionKey.OP_CONNECT);
        }
    }

    private void doWrite(SocketChannel socketChannel) throws IOException {
        byte[] req="Query time order".getBytes();

        ByteBuffer byteBuffer=ByteBuffer.allocate(req.length);
        byteBuffer.put(req);
        byteBuffer.flip();
        socketChannel.write(byteBuffer);

        if (!byteBuffer.hasRemaining()){
            System.out.println("发送成功");
        }
    }
}

AIO编程

服务端:

public class TimeServer {
    public static void main(String[] args) {
        int port=8000;

        if (args!=null && args.length>0){
            try {
                port=Integer.parseInt(args[0]);
            }catch (NumberFormatException e){

            }
        }
        AsyncServer timeServer=new AsyncServer(port);
        new Thread(timeServer,"aio-async").start();
    }
}
public class AsyncServer implements Runnable{

    private int port;
    CountDownLatch latch;
    AsynchronousServerSocketChannel asynchronousServerSocketChannel;

    public AsyncServer(int port) {
        this.port=port;
        try {
            asynchronousServerSocketChannel=AsynchronousServerSocketChannel.open();
            asynchronousServerSocketChannel.bind(new InetSocketAddress(port));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void run() {
        System.out.println("服务启动");
        latch=new CountDownLatch(1);
        doAccept();
        try {
            latch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private void doAccept() {
        asynchronousServerSocketChannel.accept(this,
                new AcceptCompletionHandler());
    }
}
public class AcceptCompletionHandler implements CompletionHandler<AsynchronousSocketChannel, AsyncServer> {
    @Override
    public void completed(AsynchronousSocketChannel result, AsyncServer attachment) {
        attachment.asynchronousServerSocketChannel.accept(attachment,this);

        java.nio.ByteBuffer buffer= java.nio.ByteBuffer.allocate(1024);
        result.read(buffer,buffer,new ReadCompletionHandler(result));
    }

    @Override
    public void failed(Throwable exc, AsyncServer attachment) {
        exc.printStackTrace();
        attachment.latch.countDown();
    }
}
public class ReadCompletionHandler implements CompletionHandler<Integer, ByteBuffer> {
    private AsynchronousSocketChannel channel;
    public ReadCompletionHandler(AsynchronousSocketChannel result) {
        if (this.channel==null){
            this.channel=result;
        }
    }

    @Override
    public void completed(Integer result, ByteBuffer attachment) {
        attachment.flip();
        byte[] body = new byte[attachment.remaining()];
        attachment.get(body);

        try {
            String req =new String(body,"UTF-8");

            String currentTime=new Date().toString();

            doWrite(currentTime);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

    private void doWrite(String currentTime) {
        if (currentTime!=null && currentTime.trim().length()>0){
            byte[] bytes=currentTime.getBytes();
            ByteBuffer wbuffer=ByteBuffer.allocate(bytes.length);

            wbuffer.put(bytes);
            wbuffer.flip();

            channel.write(wbuffer, wbuffer, new CompletionHandler<Integer, ByteBuffer>() {
                @Override
                public void completed(Integer result, ByteBuffer attachment) {
                    if (attachment.hasRemaining()){
                        channel.write(attachment,attachment,this);
                    }
                }

                @Override
                public void failed(Throwable exc, ByteBuffer attachment) {
                    try {
                        channel.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    }

    @Override
    public void failed(Throwable exc, ByteBuffer attachment) {
        try {
            this.channel.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

客户端:

public class AsyncClient {

    public static void main(String[] args) {
        int port=8000;

        if (args!=null && args.length>0){
            try {
                port=Integer.parseInt(args[0]);
            }catch (NumberFormatException e){

            }
        }
        AsyTimeClientHandler clientHandler= new AsyTimeClientHandler("127.0.0.1",port);
        new Thread( clientHandler,"aio-async").start();
    }
}
public class AsyTimeClientHandler implements Runnable, CompletionHandler<Void,AsyTimeClientHandler > {

    private AsynchronousSocketChannel client;
    private String host;
    private int port;
    private CountDownLatch latch;

    public AsyTimeClientHandler(String host, int port) {
            this.host=host;
            this.port=port;
            try {
                client=AsynchronousSocketChannel.open();
            } catch (IOException e) {
                e.printStackTrace();
            }
    }

    @Override
    public void run() {
        latch= new CountDownLatch(1);
        client.connect(new InetSocketAddress(host,port),this,this);

        try {
            latch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        try {
            client.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void completed(Void result, AsyTimeClientHandler attachment) {
        byte[] req="hello .".getBytes();
        ByteBuffer wbuf=ByteBuffer.allocate(req.length);
        wbuf.put(req);
        wbuf.flip();

        client.write(wbuf, wbuf, new CompletionHandler<Integer, ByteBuffer>() {
            @Override
            public void completed(Integer result, ByteBuffer attachment) {
                if (attachment.hasRemaining()){
                    client.write(attachment,attachment,this);
                } else {
                    ByteBuffer readBuffer=ByteBuffer.allocate(1024);

                    client.read(
                            readBuffer,
                            readBuffer,
                            new CompletionHandler<Integer, ByteBuffer>() {
                                @Override
                                public void completed(Integer result, ByteBuffer attachment) {
                                    attachment.flip();
                                    byte[] bytes=new byte[attachment.remaining()];
                                    attachment.get(bytes);

                                    String body;
                                    try {
                                        body=new String(bytes,"utf-8");
                                        System.out.println("时间是:"+body);
                                        latch.countDown();
                                    } catch (UnsupportedEncodingException e) {
                                        e.printStackTrace();
                                    }
                                }

                                @Override
                                public void failed(Throwable exc, ByteBuffer attachment) {
                                    try {
                                        client.close();
                                    } catch (IOException e) {
                                        e.printStackTrace();
                                    }
                                    latch.countDown();
                                }
                            }
                    );
                }
            }

            @Override
            public void failed(Throwable exc, ByteBuffer attachment) {
                try {
                    client.close();
                    latch.countDown();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    @Override
    public void failed(Throwable exc, AsyTimeClientHandler attachment) {
        try {
            client.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        latch.countDown();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 适合毕业设计、课程设计作业。这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。 所有源码均经过严格测试,可以直接运行,可以放心下载使用。有任何使用问题欢迎随时与博主沟通,第一时间进行解答!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值