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 understand NIO blocking communication in Netty

2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article focuses on "how to understand NIO blocking communication in Netty". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to understand NIO blocking communication in Netty.

NIO completes the three cores of network communication: Channel, Buffer and Selector.

1. Channel: responsible for connecting SocketChannel, ServerSocketChannel, DatagramChannel, Pipe.SinkChannel, Pipe.SourceChannel2, buffer (Buffer): responsible for data access 3, selector (Selector): is a SelectableChannel multiplexer. Used to monitor the IO status of SelectableChannel 1, blocking network traffic copy data / / client @ Testpublic void client () throws IOException {/ / 1. Get channel SocketChannel sChannel = SocketChannel.open (new InetSocketAddress ("127.0.0.1", 8085)); FileChannel inChannel = FileChannel.open (Paths.get ("/ Users/tentsuuhou/Desktop/777.txt"), StandardOpenOption.READ); / / 2. Allocates a buffer of the specified size ByteBuffer buf = ByteBuffer.allocate (1024); / / 3. Read the local file and send it to the server while (inChannel.read (buf)! =-1) {buf.flip (); sChannel.write (buf); buf.clear ();} / / 4. Close the channel inChannel.close (); sChannel.close ();} / / server @ Testpublic void server () throws IOException {/ / 1. Get channel ServerSocketChannel ssChannel = ServerSocketChannel.open (); FileChannel outChannel = FileChannel.open (Paths.get ("/ Users/tentsuuhou/Desktop/666.txt"), StandardOpenOption.WRITE, StandardOpenOption.CREATE); / / 2. Bind connection ssChannel.bind (new InetSocketAddress (8085)); / / 3. Get the channel of the client connection SocketChannel sChannel = ssChannel.accept (); / / 4. Allocates a buffer of the specified size ByteBuffer buf = ByteBuffer.allocate (1024); / / 5. Receive the client's data and save it to the local while (sChannel.read (buf)! =-1) {buf.flip (); outChannel.write (buf); buf.clear ();} / / 6. Close the channel sChannel.close (); outChannel.close (); ssChannel.close ();}

First open the server side, then open the client side, so that you can ok.

2. Blocking simple version client sends server data / / client @ Testpublic void client () throws IOException {SocketChannel sChannel = SocketChannel.open (new InetSocketAddress ("127.0.0.1", 8080)); ByteBuffer buf = ByteBuffer.allocate (1024); sChannel.shutdownOutput (); / / receives feedback from the server int len = 0; while ((len = sChannel.read (buf))! =-1) {buf.flip () System.out.println (new String (buf.array (), 0, len)); buf.clear ();} sChannel.close ();} / / server @ Testpublic void server () throws IOException {ServerSocketChannel ssChannel = ServerSocketChannel.open (); ssChannel.bind (new InetSocketAddress (8080)); SocketChannel sChannel = ssChannel.accept (); ByteBuffer buf = ByteBuffer.allocate (1024) / / send feedback to the client buf.put ("the server received data successfully" .getBytes ()); buf.flip (); sChannel.write (buf); sChannel.close (); ssChannel.close ();}

In client, sChannel.shutdownOutput (); without this, the server does not know that it is in a blocking state, and only shutdownOutput () can alert the server side, so that the data of client can be passed to the server.

At this point, I believe you have a deeper understanding of "how to understand NIO blocking communication in Netty". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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