2 u013179958 u013179958 于 2017.01.16 17:37 提问

怎么能让这个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;
}

1个回答

morgerton
morgerton   2017.01.16 18:43
已采纳

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

Csdn user default icon
上传中...
上传图片
插入图片