In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
What are the traditional BIO network programming knowledge points and Java NIO? aiming at this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.
I originally wanted to add this part to the article "sharing practical experience of Netty High concurrent programming and performance tuning", but I'm afraid you won't have the desire to read it after reading the previous article, .
The contents include:
Summary of traditional BIO programming knowledge points
Introduction to Java NIO
Summary of traditional BIO programming knowledge points
The picture below is the mind map of "Netty High concurrency programming and performance tuning" made by me with my impression notes. This is my summary of network programming. I came into contact with Socket programming a long time ago. I learned Socket programming in college. Although I learned Socket programming at that time, the principle is the same, regardless of language.
I have participated in the China Software Cup, and what I have done is a "synchronous hand-drawn board" application, which uses Socket to achieve synchronous rendering between the mobile end and the PC side, although I did not win a prize. During the junior college internship, I made a system to monitor the camera equipment, such as remotely controlling the camera angle, taking pictures automatically, obtaining electricity, and so on. After resigning from the first company, I made a chat system that imitates Wechat. During this period, I learned about the existence of NIO,Netty and followed the trend. Later, I also used Netty to achieve a server with private chat function of love APP. I also know a little bit about BIO and NIO. Recently, I have been eating the source code of Netty for a long time to tune the performance of the project. Although I have only scratched the surface now, it is still very helpful for this performance tuning.
My summary of traditional BIO programming, several knowledge points that I think are the most important.
Knowledge point 1: Socket socket multiplex pool
Regardless of any high-level language, Socket programming is a thread on the server side that handles a connection to the client, because both reads and writes are blocked. To avoid frequent thread creation and destruction, thread pools are used to reuse threads. For BIO, the maximum number of connections that the service can handle concurrently is estimated based on the server hardware configuration, and the size of the thread multiplexing pool is set accordingly.
Knowledge point 2: memory cache pool
The cache area used to receive and send bytes of data is also used to avoid frequently requesting and freeing memory from the system. Correspondingly, Netty also has the concept of cache pool, which is relatively complex, which is divided into direct memory and heap memory. Direct memory is jvm out-of-heap memory and is not managed by jvm.
Knowledge point 3: message queuing, parsing packets
With a memory cache pool, why should there be a message queue? When the server reads the message sent by the client, it may not be a complete data packet, so it needs to parse the received byte data, parse the byte data according to the protocol used, and parse it into a complete packet.
Knowledge point 4: custom communication protocol
As back-end developers, we are most familiar with the HTTP protocol. Using Socket to write network programs, we can customize the communication protocol, which is quite fun. Custom protocols can prevent others from identifying your communication protocol to intercept packet analysis, and can also encrypt data packets. The packet size of the custom protocol is small and can be modified according to business requirements.
Knowledge point 5: heartbeat to keep alive
TCP protocol requires three handshakes to establish a connection and four waves to release the connection. But what cannot be avoided is that the client or server is accidentally disconnected without the other party knowing it. For example, the Wifi of the client rubbing against Lao Wang next door was found. If the server writes a message to the client after the client is disconnected unexpectedly, an exception will be thrown. If the data is not read or written for a long time, the thread will always block there and occupy the thread. The heartbeat packet can not only detect whether the connection is available, but also disconnect and reconnect automatically.
In some business scenarios, we can set how long the connection has not been read or written, and the server actively disconnects. This is idle detection.
About JAVA NIO
The difference between NIO and BIO:
BIO: synchronous blocking IO, where the server needs to create a threaded connection for each client.
NIO: synchronous non-blocking IO. The server can use one or more threads to listen to the connection request of the client, register the connection to the multiplexer Selector, and use Selector to poll the I _ ready event. When a ready event is heard, a thread will be opened for the ready connection to process.
Compared with the traditional BIO model, NIO saves the cost of binding one thread for each connection, and supports establishing connections with a large number of clients at the same time, which is only limited by the maximum number of open file descriptors in the system.
NIO
Channel:channel is a channel through which data can be read and written. For network programming, network data can receive messages from the client through channel, or send messages to the client through channel. Channel is full-duplex, and the corresponding classes are SocketChannel and ServerSocketChannel.
ServerSocketChannel: a channel used to listen for TCP connections, similar to ServerSocket.
ServerSocketChannel serverSocketChannel = ServerSocketChannel.open (); / bind server listening port serverSocketChannel.socket () .bind (new InetSocketAddress (this.port), 1024); / / listen client connection SocketChannel socketChannel = serverSocketChannel.accept ()
SocketChannel: the channel used for TCP network connections. Realize the data transmission with the client. Can be set to non-blocking. Similar to Socket.
The difference between Socket and Channel: socket data flow is unidirectional, and two-way communication between client and server requires an Input stream and an Output stream, one for receiving data and one for sending data. On the other hand, Channel is full-duplex, that is, it can accept data from the client or send data to the client.
Selector: selector, or multiplexer. Used to poll to check the status of one or more NIO channels (Channel). For network programming, Socket has four states: listening connection, connection ready, read ready, and write ready. The corresponding values of SelectorKey are as follows.
SelectorKey:
OP_READ = 1
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.