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 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥30 用arduino开发esp32控制ps2手柄一直报错
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿