CSDNRGY 2017-01-16 09:37 采纳率: 87.5%
浏览 1221
已采纳

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

  • 写回答

3条回答 默认 最新

  • 白萝卜。 2017-01-16 10:43
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示