客户端启动

    1. AioClientStarter client = new AioClientStarter("127.0.0.1",8888);
    2. client.channelInitializer(new ChannelInitializer() {
    3. @Override
    4. public void initChannel(AioChannel channel) throws Exception {
    5. //责任链
    6. DefaultChannelPipeline defaultChannelPipeline = channel.getDefaultChannelPipeline();
    7. //指定结束符解码器
    8. defaultChannelPipeline.addLast(new DelimiterFrameDecoder(DelimiterFrameDecoder.lineDelimiter));
    9. //字符串解码器
    10. defaultChannelPipeline.addLast(new StringDecoder());
    11. //定义消息解码器
    12. defaultChannelPipeline.addLast(new SimpleHandler());
    13. }
    14. });
    15. try {
    16. client.start();
    17. } catch (Exception e) {
    18. e.printStackTrace();
    19. }

    通过配置启动

    1. //实例化客户端配置
    2. AioClientConfig aioConfig = new AioClientConfig();
    3. //设置远程地址
    4. aioConfig.setHost("127.0.0.1");
    5. //设置远程端口
    6. aioConfig.setPort(8888);
    7. //设置客户端端内存池最大可分配空间大小,默认256mb,内存池空间可以根据吞吐量设置。
    8. aioConfig.setClientChunkSize(512 * 1024 * 1024);
    9. //设置数据输出器队列大小,一般不用设置这个参数,默认是10*1024*1024
    10. aioConfig.setBufferWriterQueueSize(2 * 1024 * 1024);
    11. //设置读取缓存块大小,一般不用设置这个参数,默认128字节
    12. aioConfig.setReadBufferSize(128);
    13. //设置内存池等待分配内存的最大阻塞时间,默认是1秒
    14. aioConfig.setChunkPoolBlockTime(1000);
    15. //设置SocketOptions
    16. aioConfig.setOption(StandardSocketOptions.SO_RCVBUF,8192);
    17. AioClientStarter client = new AioClientStarter(aioConfig);
    18. client.channelInitializer(new ChannelInitializer() {
    19. @Override
    20. public void initChannel(AioChannel channel) throws Exception {
    21. //责任链
    22. DefaultChannelPipeline defaultChannelPipeline = channel.getDefaultChannelPipeline();
    23. //指定结束符解码器
    24. defaultChannelPipeline.addLast(new DelimiterFrameDecoder(DelimiterFrameDecoder.lineDelimiter));
    25. //字符串解码器
    26. defaultChannelPipeline.addLast(new StringDecoder());
    27. //定义消息解码器
    28. defaultChannelPipeline.addLast(new SimpleHandler());
    29. }
    30. });
    31. try {
    32. client.start();
    33. } catch (Exception e) {
    34. e.printStackTrace();
    35. }