序言:
11月之恋
光线交织的街道,滴答滴答的雨声渐远渐近,那是一个行走在人世间的修长躯体。他拥抱着一个挚爱的身体时,他知道,自己是彻底的孤独的,他所有的情欲只是无可奈何的占有。他试图用各种语言与人沟通,但他也同时知道,语言的终极只是更大的孤独。
雨声渐渐消散于地平线,他微闭的双眸有着神采在汇聚,他不曾回头看看身后的街道,风干了的泪痕刻画在外在的皮囊表层,高速运转的脑
中浮现了他过往回忆的剪辑,他的躯体还可以撑过明天,还有可供消耗能量,只要躯体内的线不断,骨骼的运转不止。
作者:blackking
正文:我的学习体会,背景,需要结合(二)一起来看,做与PLC通讯时涉及到编解码器,所以拿来共享学习经验~么么哒
服务端的编写,三个类:
第一个类:主类server
package com.blackking.netty.testhandler; import io.netty.bootstrap.ServerBootstrap; import io.netty.channel.ChannelFuture; import io.netty.channel.EventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.nio.NioServerSocketChannel; public class MyServer { public static void main(String[] args) throws Exception{ EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); try{ ServerBootstrap serverBootstrap = new ServerBootstrap(); serverBootstrap.group(bossGroup,workerGroup).channel(NioServerSocketChannel.class). childHandler(new MyServerInitializer()); /* 我们先用一个null来占据位置~~*/ ChannelFuture channelFuture = serverBootstrap.bind(8899).sync(); channelFuture.channel().closeFuture().sync(); }finally { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } } }
第二个类:handler,处理器类
package com.blackking.netty.testhandler; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.SimpleChannelInboundHandler; import java.util.UUID; /*有的时候下面这个还要实现一个父类,string可以替换的~~*/ public class MyServerhandler extends SimpleChannelInboundHandler<String> { /*<String>范型类型原本是字符串,现在我们修改一下*/ @Override protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception { /* 首先我们要把客户端的地址打印出来~这一点十分重要~*/ System.out.println(ctx.channel().remoteAddress()+","+msg); /* 这里我们要用的方法很明确~~writeAndFlush()*/ ctx.writeAndFlush(654321L); /*// 以上,我们服务器端的已经算是写完了~~*/ } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { super.exceptionCaught(ctx, cause); } }
第三个类:实现类
package com.blackking.netty.testhandler; import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelPipeline; import io.netty.channel.socket.SocketChannel; public class MyServerInitializer extends ChannelInitializer<SocketChannel>{ @Override protected void initChannel(SocketChannel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); pipeline.addLast(new MyByteToLongDecoder2()); pipeline.addLast(new MyLongToStringDecoder()); pipeline.addLast(new MyLongToByteEncoder()); //上面三个是自己添加的编解码器 pipeline.addLast(new MyServerhandler()); } }
黑眸青年喜欢听的一首歌:
when you crawl to my side
你匍匐到我身旁
you’re like a wounded bird
像一只受伤的小鸟
with your wings weighed down
翅膀低垂
you’re somehow smaller than you were
不知为何,显得沮丧又卑微
but all of it was real to me
我以为一切都是真实的
’til you showed me
可是你向我揭示
we were make-believe
我们只是虚幻
why are we not accelerating
为什么我们无法加速
pedals down
踏板已踩下
our pulses racing
脉搏飙升
particles kaleidoscopic
粒子如万花筒般千变万化
fireworks dreamed by an ion
离子梦见烟火
now the charges are laid
电荷累积
the poles have swung away
电极变换
and right at the split
分裂之时
you might just feel some pain
你可能仅会感到一丝疼痛
or maybe you will realize that
也许你将意识到
i’m small enough to slip your mind
我太渺小,无法在你的记忆中停留
we were make-believe
我们只是虚幻
why are we not accelerating
为什么我们无法加速
pedals down
踏板已踩下
our pulses racing
脉搏飙升
particles kaleidoscopic
粒子如万花筒般千变万化
fireworks dreamed by an ion
离子梦见烟火