客户端启动
AioClientStarter client = new AioClientStarter("127.0.0.1",8888);
client.channelInitializer(new ChannelInitializer() {
@Override
public void initChannel(AioChannel channel) throws Exception {
//责任链
DefaultChannelPipeline defaultChannelPipeline = channel.getDefaultChannelPipeline();
//指定结束符解码器
defaultChannelPipeline.addLast(new DelimiterFrameDecoder(DelimiterFrameDecoder.lineDelimiter));
//字符串解码器
defaultChannelPipeline.addLast(new StringDecoder());
//定义消息解码器
defaultChannelPipeline.addLast(new SimpleHandler());
}
});
try {
client.start();
} catch (Exception e) {
e.printStackTrace();
}
通过配置启动
//实例化客户端配置
AioClientConfig aioConfig = new AioClientConfig();
//设置远程地址
aioConfig.setHost("127.0.0.1");
//设置远程端口
aioConfig.setPort(8888);
//设置客户端端内存池最大可分配空间大小,默认256mb,内存池空间可以根据吞吐量设置。
aioConfig.setClientChunkSize(512 * 1024 * 1024);
//设置数据输出器队列大小,一般不用设置这个参数,默认是10*1024*1024
aioConfig.setBufferWriterQueueSize(2 * 1024 * 1024);
//设置读取缓存块大小,一般不用设置这个参数,默认128字节
aioConfig.setReadBufferSize(128);
//设置内存池等待分配内存的最大阻塞时间,默认是1秒
aioConfig.setChunkPoolBlockTime(1000);
//设置SocketOptions
aioConfig.setOption(StandardSocketOptions.SO_RCVBUF,8192);
AioClientStarter client = new AioClientStarter(aioConfig);
client.channelInitializer(new ChannelInitializer() {
@Override
public void initChannel(AioChannel channel) throws Exception {
//责任链
DefaultChannelPipeline defaultChannelPipeline = channel.getDefaultChannelPipeline();
//指定结束符解码器
defaultChannelPipeline.addLast(new DelimiterFrameDecoder(DelimiterFrameDecoder.lineDelimiter));
//字符串解码器
defaultChannelPipeline.addLast(new StringDecoder());
//定义消息解码器
defaultChannelPipeline.addLast(new SimpleHandler());
}
});
try {
client.start();
} catch (Exception e) {
e.printStackTrace();
}