怎么能让这个AioServerSocket一直开着?

现在的情况是,我客户端一向服务器发送请求,服务器能正常接收相应,然后就关闭了。。。
怎么能让他一直开着啊?

服务器代码

 package aio;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousServerSocketChannel;
import java.nio.channels.AsynchronousSocketChannel;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

public class AioServer{
    //服务器对象
    AsynchronousServerSocketChannel server;
    //连接进来的客户端集合
    Future<AsynchronousSocketChannel> clients;
    //连接进来的客户端
    AsynchronousSocketChannel client;
    //缓冲数组
    ByteBuffer readBuffer = ByteBuffer.allocate(1024);

    public static void main(String[] args) throws Exception {
        System.out.println("开启服务器");
        new AioServer().run();
    }
    public void run() throws Exception{
        //给服务器绑定地址
        server = AsynchronousServerSocketChannel.open().bind(Config.address);
        System.out.println("等待客户端连接");
        clients = server.accept();
        client = clients.get(10, TimeUnit.SECONDS);
        System.out.println(client.getRemoteAddress()+":连接进来了");
        //每连接一个客户端,就清空一次缓冲数组,防止溢出
        readBuffer.clear();
        //把客户端传的数据,放到缓冲数组中
        client.read(readBuffer).get(10, TimeUnit.SECONDS);
        System.out.println("收到了消息: " + new String(readBuffer.array()));
        //把缓冲数组的指针归0
        readBuffer.flip();
        //可客户端输出数据
        client.write(readBuffer);
        //把缓冲数组的指针归0
        readBuffer.flip();
    }
}

客户端代码

 package aio;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousSocketChannel;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class AioClient {
    public static void main(String[] args) {
        new AioClient().run();
    }
    public void run(){
        try {
            AsynchronousSocketChannel client = AsynchronousSocketChannel.open();
            System.out.println("连接服务器");
            client.connect(Config.address).get();
            String msg = "ping";
            ByteBuffer message = ByteBuffer.wrap(msg.getBytes());
            System.out.println("发送数据:" + msg);
            client.write(message).get(10, TimeUnit.SECONDS);
            System.out.println("发送成功");
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }
    }
}

公共配置文件

 package aio;
import java.net.InetSocketAddress;

public interface Config {

    //公共地址
    InetSocketAddress address = new InetSocketAddress("localhost", 5000);
    //等待时间
    long wariTime = 10;
}

楼主你好,服务器需要一直监听,你这样处理等于只可以连接一个客服端,建议使用while(true)加线程池操作,
http://blog.csdn.net/kaiwii/article/details/8540439
望采纳。

buff.clear();
//准备读取下一次数据
sc.read(buff, null, this);

AIO的channel有两种处理方式,一种future,一种handler。 查看AsynchronousSocketChannel的api接口,你会发现accpet,read,write都提供了前面说的两种处理方式的接口。用future需要调用get()来获取结果,但是是阻塞的。建议使用handler的方式来处理。
图片说明图片说明

这是我实现的aio的server,但有一个bug我还未找到解决方案,就是与client的每一个socket连接,只能通信一次。哥们你要是找到原因了,麻烦告诉我一下~