In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Editor to share with you how to solve the problem of TCP socket blocking, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's learn about it!
Solve the blocking problem of TCP socket
As we all know, the reading and writing of tcp is blocked, that is, I don't know when to finish reading and writing, so the thread has been paused. Generally, tcp programs are used between upper and lower computers, right?
Some devices in the lower computer generally send heartbeat messages to our machines, let's say once in 10 seconds. When the machine does not receive data for more than 10 seconds, then we should consider disconnecting the socket, because if the device is reconnected continuously, a new socket may be established, so that if the equipment is disconnected repeatedly, it will generate a lot of socket and occupy a lot of system resources. Here we use the socket.setSoTimeout method to solve the blocking problem of the read method and set a flag bit at the same time.
Exit socket connection public void run () {InputStream is = null; OutputStream os = null; int flag = 0; try {socket.setSoTimeout (500); / / blocking is = socket.getInputStream (); os = socket.getOutputStream ();} catch (Exception e2) {e2.printStackTrace () } while (true) {try {/ / read data int readlen = is.read (buff); if (readlen > 0) {flag = 0;} byte data [] = Arrays.copyOfRange (buff, 0, readlen); resolveData (data);} catch (IOException e) {try {flag++; if (flag = = 200) {is.close (); os.close () Monitoring and handling of socket.close ();}} catch (Exception E1) {e1.printStackTrace ();}} TCP connection blocking
So what kind of TCP connection belongs to the above blocked abnormal connection? Combine the experience of online operation and maintenance
We sort out the characteristics that match this type of anomaly as follows
The value of the Recv_Q for this connection is very large (more than 3m)
The value of the Recv_Q of the connection continues to rise, resulting in accumulation (within a certain sliding time window)
The server process has stopped processing requests for this connection for a long time (more than 90 seconds)
The value of Recv_Q can be sampled by Recv_Q queue size through netstat or ss system tools, thus the threshold can be judged.
The result of netstat is from reading the / proc/net/tcp file.
How to view the creation time of a connection
1.nestat-apn | grep xxx to view the process pid and port of the corresponding connection
two。 Convert upstream and downstream ports to hexadecimal xxxa xxxb
3. Then cat / proc/net/tcp | grep-I xxxa | grep-I xxxb finds the inode inodex of the socket connection
4.ls-al / proc/pid/fd | grep inodex can see the creation time of the socket file.
The above is all the contents of the article "how to solve the blocking problem of TCP socket". 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.
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.