欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

Netty实战指南:第四版本第10期——如何运用Netty 4.1处理不同协议类型的消息收发

最编程 2024-07-30 13:35:44
...
/** * 虫洞栈:https://bugstack.cn * 公众号:bugstack虫洞栈 {关注获取学习源码} * Create by fuzhengwei on 2019 */ public class NettyClient { public static void main(String[] args) { new NettyClient().connect("127.0.0.1", 7397); } private void connect(String inetHost, int inetPort) { EventLoopGroup workerGroup = new NioEventLoopGroup(); try { Bootstrap b = new Bootstrap(); b.group(workerGroup); b.channel(NioSocketChannel.class); b.option(ChannelOption.AUTO_READ, true); b.handler(new MyChannelInitializer()); ChannelFuture f = b.connect(inetHost, inetPort).sync(); System.out.println("itstack-demo-netty client start done. {关注公众号:bugstack虫洞栈,获取源码}"); //测试消息,分别发放demo01、demo02、demo03 f.channel().writeAndFlush(MsgUtil.buildMsgDemo01(f.channel().id().toString(),"你好,消息体MsgDemo01,我是https://bugstack.cn博主,付政委。这是我的公众号<bugstack虫洞栈>,欢迎关注我获取案例源码。")); f.channel().writeAndFlush(MsgUtil.buildMsgDemo02(f.channel().id().toString(),"你好,消息体MsgDemo02,我是https://bugstack.cn博主,付政委。这是我的公众号<bugstack虫洞栈>,欢迎关注我获取案例源码。")); f.channel().writeAndFlush(MsgUtil.buildMsgDemo03(f.channel().id().toString(),"你好,消息体MsgDemo03,我是https://bugstack.cn博主,付政委。这是我的公众号<bugstack虫洞栈>,欢迎关注我获取案例源码。")); f.channel().closeFuture().sync(); } catch (InterruptedException e) { e.printStackTrace(); } finally { workerGroup.shutdownGracefully(); } } }