Netty入门之客户端与服务端通信(二)
2017-06-12 22:45
381 查看
Netty入门之客户端与服务端通信(二)
一.简介
在上一篇博文中笔者写了关于Netty入门级的Hello World程序。书接上回,本博文是关于客户端与服务端的通信,感觉也没什么好说的了,直接上代码吧。
二.客户端与服务端的通信
2.1 服务端启动程序
2.2 服务端通道初始化程序
2.3 自定义Handler
2.4客户端启动程序
2.5客户端通道初始化
2.5客户端自定义Handler
三. 运行测试
运行服务端启动代码,然后在运行客户端启动代码,就可以看见千万只"草泥马"在崩腾。
一.简介
在上一篇博文中笔者写了关于Netty入门级的Hello World程序。书接上回,本博文是关于客户端与服务端的通信,感觉也没什么好说的了,直接上代码吧。
二.客户端与服务端的通信
2.1 服务端启动程序
public class MyServer { public static void main(String[] args) throws InterruptedException { EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); try{ ServerBootstrap serverBootstrap = new ServerBootstrap(); serverBootstrap.group(bossGroup, workerGroup) .channel(NioServerSocketChannel.class) .childHandler(new MyInitializer()); ChannelFuture channelFuture = serverBootstrap.bind(8899).sync(); channelFuture.channel().closeFuture().sync(); }finally{ bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } } }
2.2 服务端通道初始化程序
public class MyInitializer extends ChannelInitializer<SocketChannel>{ @Override protected void initChannel(SocketChannel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); /** * LengthFieldBasedFrameDecoder: 基于长度属性的帧解码器。 * 客户端传递过来的数据格式为: * BEFORE DECODE (14 bytes) AFTER DECODE (14 bytes) * +--------+----------------+ +--------+----------------+ * | Length | Actual Content |----->| Length | Actual Content | * | 0x000C | "HELLO, WORLD" | | 0x000C | "HELLO, WORLD" | * +--------+----------------+ +--------+----------------+ * 5个参数依次为:1.(maxFrameLength)每帧数据的最大长度. * 2.(lengthFieldOffset)length属性在帧中的偏移量。 * 3.(lengthFieldLength)length属性的长度,需要与客户端 LengthFieldPrepender设置的长度一致, * 值的取值只能为1, 2, 3, 4, 8 * 4.(lengthAdjustment)长度调节值, 当信息长度包含长度时候,用于修正信息的长度。 * 5.(initialBytesToStrip)在获取真实的内容的时候,需要忽略的长度(通常就是length的长度)。 * * 参考: http://blog.csdn.net/educast/article/details/47706599 */ pipeline.addLast("lengthFieldBasedFrameDecoder", new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 2, 0, 2)); /** * LengthFieldPrepender: length属性在帧中的长度。只能为1,2,3,4,8。 * 该值与对应的客户端(或者服务端)在解码时候使用LengthFieldBasedFrameDecoder中所指定的lengthFieldLength * 的值要保持一致。 */ pipeline.addLast("lengthFieldPrepender", new LengthFieldPrepender(3)); //StringDecoder字符串的解码器, 主要用于处理编码格式 pipeline.addLast("stringDecoder", new StringDecoder(CharsetUtil.UTF_8)); //StringDecoder字符串的编码器,主要用于指定字符串的编码格式 pipeline.addLast("stringEncoder", new StringEncoder(CharsetUtil.UTF_8)); pipeline.addLast(new MyHandler()); //自定义的Handler } }
2.3 自定义Handler
public class MyHandler extends SimpleChannelInboundHandler<String>{ @Override protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception { System.out.println(ctx.channel().remoteAddress() + ":" + msg); ctx.channel().writeAndFlush("from server: 草泥马"); } @Override public void handlerAdded(ChannelHandlerContext ctx) throws Exception { System.out.println(System.currentTimeMillis() + "********"); System.out.println("server handler added**********"); } @Override public void channelRegistered(ChannelHandlerContext ctx) throws Exception { System.out.println(System.currentTimeMillis() + "********"); System.out.println("server channel register****"); } @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { System.out.println(System.currentTimeMillis() + "********"); System.out.println("server channel actieve****"); } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { cause.printStackTrace(); ctx.close(); } }
2.4客户端启动程序
public class MyClient { public static void main(String[] args) throws InterruptedException { EventLoopGroup eventLoopGroup = new NioEventLoopGroup(); try{ Bootstrap bootstrap = new Bootstrap(); bootstrap.group(eventLoopGroup).channel(NioSocketChannel.class).handler(new ClientInitializer()); ChannelFuture channelFuture = bootstrap.connect("127.0.0.1", 8899).sync(); channelFuture.channel().closeFuture().sync(); }finally{ eventLoopGroup.shutdownGracefully(); } } }
2.5客户端通道初始化
public class ClientInitializer extends ChannelInitializer<SocketChannel>{ @Override protected void initChannel(SocketChannel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); pipeline.addLast("lengthFieldBasedFrameDecoder", new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 2, 0, 2)); pipeline.addLast("lengthFieldPrepender", new LengthFieldPrepender(3)); pipeline.addLast("stringDecoder", new StringDecoder(CharsetUtil.UTF_8)); pipeline.addLast("stringEncoder", new StringEncoder(CharsetUtil.UTF_8)); pipeline.addLast(new MyClientHandler()); } }
2.5客户端自定义Handler
public class MyClientHandler extends SimpleChannelInboundHandler<String>{ @Override protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception { System.out.println(ctx.channel().remoteAddress()); System.out.println(msg); ctx.channel().writeAndFlush("to Server: 草泥马"); } @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { System.out.println(System.currentTimeMillis() + "..........."); ctx.channel().writeAndFlush("来自于客户端的问候!"); System.out.println("client channel Active..."); } @Override public void handlerAdded(ChannelHandlerContext ctx) throws Exception { System.out.println(System.currentTimeMillis() + "..........."); System.out.println("client hanlder added..."); } @Override public void channelRegistered(ChannelHandlerContext ctx) throws Exception { System.out.println(System.currentTimeMillis() + "..........."); System.out.println("client channel register..."); } @Override public void channelInactive(ChannelHandlerContext ctx) throws Exception { System.out.println("client channel inactive..."); } @Override public void channelUnregistered(ChannelHandlerContext ctx) throws Exception { System.out.println("client channel unregister..."); } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { cause.printStackTrace(); ctx.close(); } }
三. 运行测试
运行服务端启动代码,然后在运行客户端启动代码,就可以看见千万只"草泥马"在崩腾。
相关文章推荐
- C# client 与java netty 服务端的简单通信,客户端采用Unity。
- C# client 与java netty 服务端的简单通信,客户端采用Unity。
- WCF客户端与服务端通信简单入门教程
- 利用Netty进行客户端和服务端交互的入门示例
- <Netty>(六)(入门篇)noi服务端序列图代码和客户端序列图代码
- AIDL使用入门, 客户端通过AIDL与服务端通信
- WCF客户端与服务端通信简单入门教程
- Netty服务端和客户端搭建(入门)
- Netty框架服务端主动向客户端通信
- WCF客户端与服务端通信简单入门教程
- Netty4 学习笔记之一:客户端与服务端通信 demo
- Netty实现客户端和服务端的通信
- android蓝牙开发入门到精通3---服务端客户端通信
- socket 通信 入门3 android 客户端 C# 服务端
- netty入门学习(3)-客户端写消息,服务端应答
- Apache XML-RPC入门:使用java搭建服务端和客户端
- wince win32 c++ socket 服务端 客户端 创建与通信
- 仿QQ聊天程序SOCKET编程服务端与客户端,成功通信
- Linux/Unix服务端和客户端Socket编程入门实例(含源码下载)
- 最基本的Socket编程(服务端跟客户端通信) C#版