Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement the server Channel of Netty does not support write operations

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you how to achieve Netty server Channel does not support write operations, I believe that most people do not understand, so share this article for your reference, I hope you will learn a lot after reading this article, let's go to know it!

As the server Channel of Netty, namely NioServerSocketChannel, it is responsible for listening to ACCEPT events. When a client connects to the server, NioServerSocketChannel is responsible for reading the ACCEPT request. Since as a Channel, both read and write operations are supported. Then the NioServerSocketChannel does not support write operations. When you want to write data to the outside through the NioServerSocketChannel, an exception will be thrown inside the Netty, and the Netty will catch the exception and will not throw the exception to the program.

@ Overridepublic void channelActive (ChannelHandlerContext ctx) throws Exception {ctx.writeAndFlush ("this is test1"); super.channelActive (ctx);}

In the above code, when NioServerSocketChannel completes the port binding, it will call back channelActive. In this method, we call the writeAndFlush method to write data to the outside. Of course, there is no external connection at this time, but it does not affect us to write data to the outside.

Eventually this write operation will call the write method in HeadContext

@ Overridepublic void write (ChannelHandlerContext ctx, Object msg, ChannelPromise promise) {unsafe.write (msg, promise);} continues to call the unsafe.write method

And NioServerSocketChanenl rewrote the filterOutboundMessage method.

An exception is thrown directly, and write operations are not allowed. The above is all the contents of the article "how to implement the server Channel of Netty does not support write operations". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report