物联网之路-启航-1

物联网之路-启航-1

昨天我们整了个服务端的思路以及代码,今给大家整个客户端实例,在之前的项目经验中,客户端的创建是及其重要,特别是多客户端的创建,

// Bootstrap,且构造函数变化很大,这里用无参构造。
Bootstrap bootstrap = new Bootstrap();
// 指定channel[通道]类型
bootstrap.channel(NioSocketChannel.class);
// 指定Handler [操纵者]
bootstrap.handler(new ChannelInitializer<channel>() {

@Override
protected void initChannel(Channel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();

/*
* 这个地方的 必须和服务端对应上。否则无法正常解码和编码
*
*/
pipeline.addLast("framer", new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));
pipeline.addLast("decoder", new StringDecoder());
pipeline.addLast("encoder", new StringEncoder());

// 客户端的逻辑,自己对数据处理
pipeline.addLast(new HelloClientHandler());
}
});
// 指定EventLoopGroup [事件 组]
bootstrap.group(new NioEventLoopGroup());

// 连接到本地的8000端口的服务端
bootstrap.connect(new InetSocketAddress("127.0.0.1", 8000));


HelloClientHandler 类

/**
* 客户端的逻辑,自己对数据处理
*
* @author flm
* 2017年11月10日
*/
private static class HelloClientHandler extends ChannelInboundHandlerAdapter {

/*
* 监听 服务器 发送来的数据
*/
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {

System.out.println("Server say : " + msg.toString());

}

/*
* 启动客户端 时触发
*/
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
System.out.println("Client active ");
ctx.writeAndFlush("我是 client " + new Date() + "\\n");
super.channelActive(ctx);
}

/*
* 关闭 客户端 触发
*/
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
System.out.println("Client close ");
super.channelInactive(ctx);
}
}

总结:客户端以服务端的创建差别在于 Bootstrap 和 serverBootstrap 以及客户端是需要添加IP的这样客户端才知道往哪里发送呗/<channel>


分享到:


相關文章: