dotnetty java netty,记在运用DotNetty的一次关于通道的调试经历

起因

最近由于工作需要通信功能,我就上午搜搜大厂都在用用什么通信框架,在java领域netty是相当火的一个通信框架,于是我就在github上找到了C#版本额netty框架---DotNetty。

DotNetty

DotNetty是微软的Azure团队,使用C#实现的Netty的版本发布。不但使用了C#和.Net平台的技术特点,并且保留了Netty原来绝大部分的编程接口。让我们在使用时,完全可以依照Netty官方的教程来学习和使用DotNetty应用程序。DotNetty拥有一个强大的后台,所以我感觉这个C#版的Netty框架也会火起来的。

第一个应用

DotNetty带着Netty的诸多光环,还有一个强大的“爹”,于是说干就干,开始写了一个TCP连接的Listener程序,代码如下:

ProtocolV1ServiceFrameHandler frameHandler = new ProtocolV1ServiceFrameHandler();

frameHandler.ProtocolMsgReceivedChanged += FrameDecoder_ProtocolMsgReceivedChanged;

BYHXTcpIdleHandler tcpIdleHandler = new BYHXTcpIdleHandler(0, 0, 5);

tcpIdleHandler.LostConnectionChanged += TcpIdleHandler_LostConnectionChanged;

group = new MultithreadEventLoopGroup();

return new ServerBootstrap()

.Group(group)

.Channel()

.Option(ChannelOption.TcpNodelay, true)

.Option(ChannelOption.SoKeepalive,true)

.Option(ChannelOption.SoBacklog,100)

.ChildHandler(new ActionChannelInitializer(channel =>

{

IChannelPipeline pipeline = channel.Pipeline;

pipeline.AddLast("Idle", tcpIdleHandler);

pipeline.AddLast("framing-enc", new LengthFieldPrepender(2));

pipeline.AddLast("en", new ProtocolV1FrameEncoder());

pipeline.AddLast("framing-dec", new LengthFieldBasedFrameDecoder(int.MaxValue, 0, 2, 0, 2));

pipeline.AddLast("dn", new ProtocolV1FrameDecoder());

pipeline.AddLast("handler", frameHandler);

}));

上面的代码看起来没有任何问题,于是我就找了一个TCP客户端模拟器,可以连接没有问题,我又打开了一个TCP客户端,问题出现了,我通过cmd(netstat -an)命令行工具看到我第二次打开的连接是TIME_WAIT,之后打开的连接都是这个状态,我百思不得其解,于是我打印了netty的log,添加如下代码:

InternalLoggerFactory.DefaultFactory.AddProvider(new ConsoleLoggerProvider((s, level) => true, false));

通过Console窗口看到的log发现第二个之后的连接****ProtocolV1ServiceFrameHandler****通道处理器初始化失败了,我对比了事例发现在实例化通道处理器的又唯一的区别,如下:

pipeline.AddLast("handler", new ProtocolV1ServiceFrameHandler());

这,,,有区别吗?不明白。忽然,我记得我查看ChannelHandlerAdapter可用的重载方法是看到一个可重载的属性IsSharable,表示通道是否可以共享,于是我把这个字段设置为true。OK!!!可以了。

总结

虽然这次调试通过了,但是一个通道那两中声明方式的差异性,或者说Netty框架处理的差异性,有何区别,我目前不得而知!我一直坚信,不刨根问底的程序员不是好的程序员,我还坚信,源码面前了无秘密。因此,我接下来会对netty的通道机制做一次深入的源码级别的刨析......

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个使用Java Netty NIO编写的TCP服务器的基本示例代码: ```java import io.netty.bootstrap.ServerBootstrap; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelInitializer; import io.netty.channel.EventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioServerSocketChannel; public class TCPServer { private final int port; public TCPServer(int port) { this.port = port; } public void run() throws Exception { EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); try { ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup) .channel(NioServerSocketChannel.class) .childHandler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new TCPServerHandler()); } }); ChannelFuture f = b.bind(port).sync(); f.channel().closeFuture().sync(); } finally { workerGroup.shutdownGracefully(); bossGroup.shutdownGracefully(); } } public static void main(String[] args) throws Exception { int port = Integer.parseInt(args[0]); new TCPServer(port).run(); } } ``` 在这个示例中,我们使用了Java Netty框架,它提供了一种快速构建网络应用程序的方式。我们创建了一个名为“TCPServer”的类,它包含一个构造函数和三个方法:run(),main()和一个自定义的“TCPServerHandler”类。 在“run()”方法中,我们创建了两个事件循环组:bossGroup和workerGroup。bossGroup接受来自客户端的连接,workerGroup负责处理实际的I/O操作。然后,我们创建了一个ServerBootstrap实例,并将bossGroup和workerGroup分别设置为其组。接下来,我们指定了服务器通道类型(NioServerSocketChannel),并设置了一个ChannelInitializer,该初始化程序将添加一个自定义的TCPServerHandler来处理来自客户端的消息。 在“main()”方法中,我们将创建一个新的TCPServer实例,并调用其“run()”方法。在该方法中,我们绑定服务器到指定的端口,并等待直到服务器通道关闭。 最后,我们有一个自定义的“TCPServerHandler”类,它实现了Netty的ChannelInboundHandler接口。该处理程序将处理来自客户端的消息,并在收到消息时向客户端发送响应。以下是该处理程序类的示例代码: ```java import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundHandlerAdapter; public class TCPServerHandler extends ChannelInboundHandlerAdapter { @Override public void channelRead(ChannelHandlerContext ctx, Object msg) { ByteBuf in = (ByteBuf) msg; try { while (in.isReadable()) { System.out.print((char) in.readByte()); System.out.flush(); } } finally { in.release(); } } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { cause.printStackTrace(); ctx.close(); } } ``` 在这个示例中,我们实现了Netty的ChannelInboundHandler接口,并重写了其中的“channelRead()”和“exceptionCaught()”方法。在“channelRead()”方法中,我们接收来自客户端的消息,并将其打印到控制台。在“exceptionCaught()”方法中,我们捕获任何异常并关闭通道。 这就是一个简单的Java Netty NIO TCP服务器的基本示例。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值