Usage of Socket Descriptor (socket)

Source: Internet
Author: User

Sockets are abstractions of communication endpoints. Just as using file descriptors to access a file, the application accesses the socket with a socket descriptor. The socket descriptor is considered to be a file descriptor in a UNIX system. In fact, many functions that handle file descriptors (read and write) can be used to handle socket descriptors. --Advanced programming for the Environment of Unix


Most of the communication between the Web application client (browser or app, etc.) and the server is done through the HTTP or HTTPS protocol. The socket (socket) should be considered as the abstraction layer of the application layer and the transport layer in the OSI seven-layer network model, which is a programming interface for the application layer. The Http/https is located in the application layer, that is, through the socket socket (socket) for information communication. As a result, every HTTP link to a web app is tied to a socket (socket), especially for applications with large concurrency, and socket (socket) usage is critical. After all, it is a "mouth" for application access. Improperly handled, it becomes an application system bottleneck.

Socket (socket) bottleneck, in most cases, a bottleneck in the socket descriptor (too many open files). As the beginning says: The socket is the abstraction of the communication endpoint, and the application of communication to the Internet is done by reading and writing to this abstraction, just like reading and writing ordinary files. In Unix operating systems, files are read and written, and the system is assigned file identifiers: file descriptors. Socket (socket) is no exception.


Talk is cheap,show your code:


I wrote the socket server code in Java Locally (window) and started the service:


public class Socketserver{public static void Main (string[] args) {Try{serversocket server=null;server = new ServerSocket ( 8000); Socket Socket=null;while (True) {socket=server.accept (); BufferedReader br = new BufferedReader (New InputStreamReader (Socket.getinputstream ())); PrintWriter pw = new PrintWriter (Socket.getoutputstream ()); System.out.println (Br.readline ());p w.println ("OK");p W.flush ();p w.close (); Br.close ();}} catch (Exception e) {e.printstacktrace ();}}}

In addition, a socket client is written in Java, tested under the test machine (SUSE Linux), packaged into jars, and observed as a consumption of the socket descriptor:


public class Socketclient{public static void Main (string[] args) throws Exception {socket socket=new socket ("192.168.22.1 5 ", 8000); Socket socket2=new socket ("192.168.22.15", 8000); BufferedReader br=new BufferedReader (New InputStreamReader (Socket.getinputstream ())); BufferedReader br2=new BufferedReader (New InputStreamReader (Socket2.getinputstream ())); PrintWriter pw = new PrintWriter (Socket.getoutputstream ()); PrintWriter pw2 = new PrintWriter (Socket2.getoutputstream ());p w.println ("Hello");p w2.println ("Hello2");p W.flush (); Pw2.flush (); System.out.println (Br.readline ()); System.out.println (Br2.readline ()); Thread.Sleep (20000); Br.close (); Br2.close ();p w.close ();p w2.close (); Socket.close (); Socket2.close (); SYSTEM.OUT.PRINTLN ("socket close"); Thread.Sleep (20000); System.out.println ("function Close");}}


During the above code run, I will observe the number of file descriptors consumed by this socketclient process through the lsof-p <socketclient process number >|wc–l command.

Start Socketclient.jar20 seconds ago (OK):

Results: 101

Start socketclient.jar20 seconds to 40 seconds (socket close):

Results: 99

After starting Socketclient.jar40 seconds (function Close):

Results: 0


Start socketclient.jar20 seconds ago lsof-p <socketclient Process number > Details:


COMMAND PID USER FD TYPE DEVICE size/off NODE NAME
Java 5920 root cwd DIR 8,3 4096 486764/usr/local/test
Java 5920 root RTD DIR 8,3 4096 2/
Java 5920 root txt REG 8,3 59141 878312/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/bin/java
Java 5920 root mem REG 8,3 116348 878486/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/amd64/libnet.so
Java 5920 root mem REG 8,3 88544 90416/lib64/libgcc_s.so.1
Java 5920 root mem REG 8,3 84914 878492/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/amd64/libzip.so
Java 5920 root mem REG 8,3 9975 878465/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/amd64/libdbgwrapper.so
Java 5920 root mem REG 8,3 108248 90308/lib64/libnsl-2.11.3.so
Java 5920 root mem REG 8,3 224723 878477/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/amd64/libjava.so
Java 5920 root mem REG 8,3 140261 878396/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/amd64/compressedrefs/li Bj9ute24.so
Java 5920 root DEL REG 8,3 2183413/var/run/nscd/dbzxax8m
Java 5920 root mem REG 8,3 90320 878429/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/amd64/default/libiverel 24.so
Java 5920 root mem REG 8,3 629488 878451/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/amd64/default/libjclsca R_24.so
Java 5920 root mem REG 8,3 225264 878449/USR/LIB64/JVM/JAVA-1_6_0-IBM-1.6.0/JRE/LIB/AMD64/DEFAULT/LIBJ9VRB2 4.so
Java 5920 root mem REG 8,3 316148 878441/USR/LIB64/JVM/JAVA-1_6_0-IBM-1.6.0/JRE/LIB/AMD64/DEFAULT/LIBJ9JVMT I24.so
Java 5920 root mem REG 8,3 187277 878433/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/amd64/default/libj9dyn2 4.so
Java 5920 root mem REG 8,3 841499 878434/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/amd64/default/libj9gc24 . So
Java 5920 root mem REG 8,3 109715 878430/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/amd64/default/libj9bcv2 4.so
Java 5920 root mem REG 8,3 6235544 878438/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/amd64/default/libj9jit2 4.so
Java 5920 root mem REG 8,3 93053 878446/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/amd64/default/libj9trc2 4.so
Java 5920 root mem REG 8,3 74373 878399/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/amd64/compressedrefs/li Bj9zlib24.so
Java 5920 root mem REG 8,3 205374 878432/USR/LIB64/JVM/JAVA-1_6_0-IBM-1.6.0/JRE/LIB/AMD64/DEFAULT/LIBJ9DMP2 4.so
Java 5920 root mem REG 8,3 35728 436315/usr/lib64/libnuma.so.1
Java 5920 root mem REG 8,3 15691 878377/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/amd64/compressedrefs/li Bhythr.so
Java 5920 root mem REG 8,3 46239 878376/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/amd64/compressedrefs/li Bhyprtshim24.so
Java 5920 root mem REG 8,3 6957 878403/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/amd64/compressedrefs/li Bvmi.so
Java 5920 root mem REG 8,3 47241 90327/lib64/librt-2.11.3.so
Java 5920 root mem REG 8,3 259675 878391/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/amd64/compressedrefs/li Bj9prt24.so
Java 5920 root mem REG 8,3 22489 878385/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/amd64/compressedrefs/li Bj9hookable24.so
Java 5920 root mem REG 8,3 70975 878394/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/amd64/compressedrefs/li Bj9thr24.so
Java 5920 root mem REG 8,3 712429 878448/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/amd64/default/libj9vm24 . So
Java 5920 root mem REG 8,3 164469 878453/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/amd64/default/libjvm.so
Java 5920 root mem REG 8,3 86110 878311/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/bin/j9vm/libjvm.so
Java 5920 root mem REG 8,3 541821 90305/lib64/libm-2.11.3.so
Java 5920 root mem REG 8,3 1754140 90297/lib64/libc-2.11.3.so
Java 5920 root mem REG 8,3 19149 90303/lib64/libdl-2.11.3.so
Java 5920 root mem REG 8,3 42772 878460/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/amd64/jli/libjli.so
Java 5920 root mem REG 8,3 135690 90323/lib64/libpthread-2.11.3.so
Java 5920 root mem REG 8,3 151051 90290/lib64/ld-2.11.3.so
Java 5920 root mem REG 8,3 217016 2906402/var/run/nscd/passwd
Java 5920 root mem REG 8,3 26050 576479/usr/lib64/gconv/gconv-modules.cache
Java 5920 root mem REG 8,3 257156 576144/usr/lib/locale/zh_cn.utf8/lc_ctype
Java 5920 root mem REG 8,3 14684 878401/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/amd64/compressedrefs/li Bjsig.so
Java 5920 root 0u CHR 136,0 0t0 3/dev/pts/0
Java 5920 root 1u CHR 136,0 0t0 3/dev/pts/0
Java 5920 root 2u CHR 136,0 0t0 3/dev/pts/0
Java 5920 root 3r REG 8,3 416697 878426/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/amd64/default/jclsc160/ Vm.jar
Java 5920 root 4r REG 8,3 10160 878493/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/annotation.jar
Java 5920 root 5r REG 8,3 189574 878494/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/beans.jar
Java 5920 root 6r REG 8,3 435869 878550/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/java.util.jar
Java 5920 root 7r REG 8,3 216671 878556/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/jndi.jar
Java 5920 root 8r REG 8,3 54586 878558/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/logging.jar
Java 5920 root 9r REG 8,3 287085 878567/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/security.jar
Java 5920 root 10r REG 8,3 128157 878569/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/sql.jar
Java 5920 root 11r REG 8,3 1198418 878542/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/ibmorb.jar
Java 5920 root 12r REG 8,3 426065 878543/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/ibmorbapi.jar
Java 5920 root 13r REG 8,3 536255 878536/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/ibmcfw.jar
Java 5920 root 14r REG 8,3 20511988 878566/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/rt.jar
Java 5920 root 15r REG 8,3 9601775 878496/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/charsets.jar
Java 5920 root 16r REG 8,3 501898 878565/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/resources.jar
Java 5920 root 17r REG 8,3 1161371 878544/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/ibmpkcs.jar
Java 5920 root 18r REG 8,3 133770 878534/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/ibmcertpathfw.jar
Java 5920 root 19r REG 8,3 27626 878538/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/ibmjgssfw.jar
Java 5920 root 20r REG 8,3 60208 878540/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/ibmjssefw.jar
Java 5920 root 21r REG 8,3 10966 878545/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/ibmsaslfw.jar
Java 5920 root 22r REG 8,3 97697 878537/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/ibmjcefw.jar
Java 5920 root 23r REG 8,3 932373 878539/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/ibmjgssprovider.jar
Java 5920 root 24r REG 8,3 442208 878541/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/ibmjsseprovider2.jar
Java 5920 root 25r REG 8,3 357168 878535/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/ibmcertpathprovider.jar
Java 5920 root 26r REG 8,3 152611 878546/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/ibmxmlcrypto.jar
Java 5920 root 27r REG 8,3 375 878560/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/management-agent.jar
Java 5920 root 28r REG 8,3 8915002 878575/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/xml.jar
Java 5920 root 29r REG 8,3 91126 878555/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/jlm.jar
Java 5920 root 30r REG 8,3 363834 878551/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/javascript.jar
Java 5920 root 31r REG 8,3 240130 886599/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/ext/ibmjcefips.jar
Java 5920 root 32r REG 8,3 1273 486857/usr/local/test/socketclient.jar
Java 5920 root 33r REG 8,3 554639 886594/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/ext/dtfj.jar
Java 5920 root 34r REG 8,3 153450 886604/USR/LIB64/JVM/JAVA-1_6_0-IBM-1.6.0/JRE/LIB/EXT/IBMXMLENCPROVIDER.J Ar
Java 5920 root 35r REG 8,3 277912 886606/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/ext/jdmpview.jar
Java 5920 root 36r REG 8,3 481038 886589/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/ext/ibmkeymanagementser Ver.jar
Java 5920 root 37r REG 8,3 232590 886601/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/ext/ibmkeycert.jar
Java 5920 root 38r REG 8,3 183719 886588/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/ext/cmpcrmf.jar
Java 5920 root 39r REG 8,3 206636 886598/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/ext/ibmcmsprovider.jar
Java 5920 root 40r REG 8,3 17081 886593/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/ext/dtfj-interface.jar
Java 5920 root 41r REG 8,3 50129 886605/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/ext/jaccess.jar
Java 5920 root 42r REG 8,3 20228 886590/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/ext/ibmsecurerandom.jar
Java 5920 root 43r REG 8,3 23831 886608/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/ext/xmlencfw.jar
Java 5920 root 44r REG 8,3 353513 886602/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/ext/ibmpkcs11impl.jar
Java 5920 root 45r REG 8,3 1202552 886596/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/ext/gskikm.jar
Java 5920 root 46r REG 8,3 148480 886595/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/ext/dtfjview.jar
Java 5920 root 47r REG 8,3 8230 886592/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/ext/dnsns.jar
Java 5920 root 48r REG 8,3 112283 886597/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/ext/healthcenter.jar
Java 5920 root 49r REG 8,3 67395 886603/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/ext/ibmsaslprovider.jar
Java 5920 root 50r REG 8,3 992455 886600/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/ext/ibmjceprovider.jar
Java 5920 root 51r REG 8,3 67801 886591/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/ext/javadiagnosticscoll Ector.jar
Java 5920 root 52r REG 8,3 468772 886607/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/ext/localedata.jar
Java 5920 root 53r REG 8,3 1273 486857/usr/local/test/socketclient.jar
Java 5920 root 54u IPv6 1565663 0t0 TCP node1:48707->192.168.22.15:irdmi (close_wait)
Java 5920 root 55u sock 0,7 0t0 1565661 can ' t identify protocol
Java 5920 root 56u IPv6 1571368 0t0 TCP node1:48708->192.168.22.15:irdmi (close_wait)


Node TCP is the consumption of socket descriptors generated by two socketclient, and the other 99 file descriptors, which are open to files related to socket functionality. When the socket is closed, the socket descriptor for node TCP is not reclaimed by the system. Therefore, if you forget to close the socket when using sockets (Scoket), not only consumes memory, it is easier to put the system to the user's maximum number of open files consumed, it will affect the normal operation of the application.

Usage of Socket Descriptor (socket)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.