1.建立socket连接

public class CmdClient {private ClientBootstrap bootstrap;private ChannelFuture channelFuture;private Channel channel;private static final CmdClient client = new CmdClient(); //单例实现,用来取channelprivate CmdClient(){}public static CmdClient getInstance(){return client;}public Channel getChannel() {return channel;} public void setChannel(Channel channel) {this.channel = channel;} public ClientBootstrap getBootstrap() {return bootstrap;} public void setBootstrap(ClientBootstrap bootstrap) {this.bootstrap = bootstrap;} public ChannelFuture getChannelFuture() {return channelFuture;} public void setChannelFuture(ChannelFuture channelFuture) {this.channelFuture = channelFuture;} public int start(String ip,int port) {System.setProperty("java.net.preferIPv4Stack", "true");System.setProperty("java.net.preferIPv6Addresses", "false");bootstrap = new ClientBootstrap(new NioClientSocketChannelFactory(Executors.newCachedThreadPool(),Executors.newCachedThreadPool()));bootstrap.setPipelineFactory(new CmdPipelineFactory());bootstrap.setOption("tcpNoDelay", true);bootstrap.setOption("keepAlive", true);channelFuture = bootstrap.connect(new InetSocketAddress(ip, port));channelFuture.awaitUninterruptibly();channel = channelFuture.awaitUninterruptibly().getChannel();return 1;} public void stop() {channelFuture.awaitUninterruptibly();if (!channelFuture.isSuccess()) {channelFuture.getCause().printStackTrace();}channelFuture.getChannel().getCloseFuture().awaitUninterruptibly();bootstrap.releaseExternalResources();}

2.CmdPipelineFactory:

public class CmdPipelineFactory implements ChannelPipelineFactory {@Overridepublic ChannelPipeline getPipeline() throws Exception {ChannelPipeline pipeline = Channels.pipeline();pipeline.addLast("cmdDecoder", new CmdDecoder());pipeline.addLast("handler", new CmdClientHandler());return pipeline;}}

3.引入CmdDecoder用来解析协议

public class CmdDecoder extends FrameDecoder {@Overrideprotected Object decode(ChannelHandlerContext ctx, Channel channel,        ChannelBuffer buffer) throws Exception {    if (buffer.readableBytes() < 1) {        return null;    }    buffer.markReaderIndex();    ChannelBuffer magicBuff = buffer.readBytes(1);    int magic = DataConvert.byteArrayToSignedInt(magicBuff            .array());    if (magic == 0x77) {        if (buffer.readableBytes() < 19) {            return null;        }        buffer.markReaderIndex();        ChannelBuffer lenBuff = buffer.readBytes(19);        PacketParse parse = new PacketParse(null);        byte[] lenByte = parse.getPackForTwoByte(lenBuff.array(), 17, 2);        int len = DataConvert.byteArrayToSignedInt(lenByte);        if (buffer.readableBytes() < len) {            buffer.resetReaderIndex();            return null;        }        ChannelBuffer frame = buffer.readBytes(len);        return frame;    }    buffer.clear();    return null;}

更多相关文章

  1. Android(安卓)Gson 泛型解析
  2. android 中如何解析Rss订阅的xml文件
  3. Android高级进阶之路【六】Android(安卓)Framework解析
  4. 在android中举例说明如何用WebView.loadUri();来打开网页
  5. Android(安卓)JSON数据的解析与封装小Demo
  6. Android(安卓)创建与解析XML(五)—— Dom4j方式
  7. Android系统架构解析
  8. Google工程师解析Android系统架构
  9. android lru缓存 辅助类LruCache源码解析

随机推荐

  1. Android—TextView的XML属性和方法
  2. Android与H5混合开发「kotlin,WebView」
  3. Android(安卓)TextView属性详解
  4. [Android] 图片裁剪总结——调用系统裁剪
  5. Android(安卓)中Animation简单例子
  6. Android开发干货大全(持续更新)
  7. Android和iOS自带的人脸检测API
  8. android中的content provider的使用
  9. Binder详解
  10. Android(安卓)Launcher 分析