DS623089578 2019-04-03 18:48 采纳率: 33.3%
浏览 258

AIO问题请帮忙解答一下,为什么会阻塞掉

问题描述:
1. 先启动服务端进入等待
2. 启动客户端进行数据发送,然后服务端与客户端线程都会被阻塞

这里是服务端代码:

package org.morningdew.javaaio.main;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousServerSocketChannel;
import java.nio.channels.AsynchronousSocketChannel;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

public class Server {
    public static void main(String[] args) {
        AsynchronousServerSocketChannel assc = null;
        try {
            assc = AsynchronousServerSocketChannel.open();
            assc.bind(new InetSocketAddress(9090));
            System.out.println("server start at 9090 !");
            while(true) {
                System.out.println("Server waiting connect !");
                Future<AsynchronousSocketChannel> f = assc.accept();
                AsynchronousSocketChannel asc = f.get();
                System.out.println(Thread.currentThread().getName());
                ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
                byteBuffer.clear();
                StringBuilder stringBuilder = new StringBuilder();
                while(asc.read(byteBuffer).get() > 0) {
                    byteBuffer.flip();
                    byte[] b = new byte[byteBuffer.remaining()];
                    byteBuffer.get(b);
                    byteBuffer.clear();
                    stringBuilder.append(new String(b, "utf-8"));
                }
                System.out.println(stringBuilder.toString());
                ByteBuffer byteBufferWrite = ByteBuffer.allocate(1024);
                byteBufferWrite.clear();
                byteBufferWrite.put("我服务点".getBytes("utf-8"));
                byteBufferWrite.flip();
                while(byteBufferWrite.hasRemaining()) {
                    System.out.println("写出成功");
                    asc.write(byteBufferWrite).get();
                }
            }

        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }finally {
            try {
                if(assc != null && assc.isOpen()) {
                    assc.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}


下是客户端代码:



package org.morningdew.javaaio.main;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousSocketChannel;
import java.util.concurrent.ExecutionException;

public class Client {
    public static void main(String[] args) {
        AsynchronousSocketChannel asc = null;
        try {
            asc = AsynchronousSocketChannel.open();
            asc.connect(new InetSocketAddress(9090)).get();
            ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
            byteBuffer.clear();
            byteBuffer.put("我是客户端".getBytes("utf-8"));
            byteBuffer.flip();
            while(byteBuffer.hasRemaining()) {
                asc.write(byteBuffer).get();
            }
            ByteBuffer byteBufferRead = ByteBuffer.allocate(1024);
            byteBufferRead.clear();
            StringBuilder stringBuilder = new StringBuilder();
            System.out.println("等到接受服务端数据");
            while(asc.read(byteBufferRead).get() > 0) {
                byteBufferRead.flip();
                byte[] b = new byte[byteBufferRead.remaining()];
                byteBufferRead.get(b);
                byteBufferRead.clear();
                stringBuilder.append(new String(b, "utf-8"));
            }
            System.out.println(stringBuilder.toString());
            System.out.println("zhixing wanbu ");
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }finally {
            try {
                if(asc != null && asc.isOpen()) {
                    asc.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 怎么在stm32门禁成品上增加记录功能
    • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
    • ¥50 NT4.0系统 STOP:0X0000007B
    • ¥15 想问一下stata17中这段代码哪里有问题呀
    • ¥15 flink cdc无法实时同步mysql数据
    • ¥100 有人会搭建GPT-J-6B框架吗?有偿
    • ¥15 求差集那个函数有问题,有无佬可以解决
    • ¥15 【提问】基于Invest的水源涵养
    • ¥20 微信网友居然可以通过vx号找到我绑的手机号
    • ¥15 解riccati方程组