Socket descriptor usage

Source: Internet
Author: User

Socket descriptor usage

Socket is the abstraction of the Communication endpoint. Just as using a file descriptor to access a file, an application uses a socket descriptor to access the socket. Socket descriptors are treated as file descriptors in UNIX systems. In fact, many functions (read and write) that process file descriptors can be used to process socket descriptors. -- Advanced Programming in unix environment

Most WEB application clients (browsers or APPs) Communicate with the server over HTTP or HTTPS. Socket should be regarded as an abstract layer directly between the application layer and the transport layer in the OSI Layer-7 network model. It is a programming interface for the application layer. HTTP/HTTPS is located at the application layer, that is, information communication through Socket. Therefore, each HTTP link of a WEB application is closely related to Socket, especially for applications with a large concurrency. The use of Socket is crucial. After all, it is a "Port" for inbound and outbound applications ". Improper handling becomes the bottleneck of the application system.

 

The bottleneck of the Socket, in most cases, is also a bottleneck of the Socket Descriptor (the number of files opened too many ). As mentioned at the beginning: socket is the abstraction of the Communication endpoint, and applications perform read and write on the Internet through the abstraction, just like reading and writing common files. In a unix operating system, to read and write files, the system needs to allocate the file identifier: file descriptor. 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());pw.println("ok");pw.flush();pw.close();br.close();}}catch(Exception e){e.printStackTrace();}}}

In addition, a Socket Client is written in Java, packaged as a jar, and put it in the test machine (SUSE Linux) for testing, and observe the consumption of the Socket descriptor:

 

public class SocketClient{public static void main(String[] args) throws Exception {Socket socket=new Socket("192.168.22.15",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());pw.println("hello");pw2.println("hello2");pw.flush();pw2.flush();System.out.println(br.readLine());System.out.println(br2.readLine());Thread.sleep(20000);br.close();br2.close();pw.close();pw2.close();socket.close();socket2.close();System.out.println("socket close");Thread.sleep(20000);System.out.println("function close");}}

 

During the above Code Execution, I will use the lsof-p <SocketClient process number> | wc-l command to observe the number of file descriptors consumed by the SocketClient process.

Start SocketClient. jar20 seconds ago (OK ):

Result: 101

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

Result: 99

After SocketClient. jar40 seconds is started (function close ):

Result: 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 4096 486764/usr/local/test
Java 5920 root rtd DIR 8, 3 4096 2/
Java 5920 root txt REG 59141 878312/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/bin/java
Java 5920 root mem REG 116348 878486/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/amd64/libnet. so
Java 5920 root mem REG 88544 90416/lib64/libgcc_s.so.1
Java 5920 root mem REG 84914 878492/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/amd64/libzip. so
Java 5920 root mem REG 9975 878465/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/amd64/libdbgwrapper. so
Java 5920 root mem REG 108248 90308/lib64/libnsl-2.11.3.so
Java 5920 root mem REG 224723 878477/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/amd64/libjava. so
Java 5920 root mem REG 140261 878396/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/amd64/compressedrefs/libj9ute24. so
Java 5920 root del reg 2183413/var/run/nscd/dbZxaX8m
Java 5920 root mem REG 90320 878429/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/amd64/default/libiverel24.so
Java 5920 root mem REG 629488 878451/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/amd64/default/libjclscar_24.so
Java 5920 root mem REG 225264 878449/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/amd64/default/libj9vrb24. so
Java 5920 root mem REG 316148 878441/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/amd64/default/libj9jvmti24. so
Java 5920 root mem REG 187277 878433/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/amd64/default/libj9dyn24. so
Java 5920 root mem REG 841499 878434/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/amd64/default/libj9gc24. so
Java 5920 root mem REG 109715 878430/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/amd64/default/libj9bcv24. so
Java 5920 root mem REG 6235544 878438/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/amd64/default/libj9jit24. so
Java 5920 root mem REG 93053 878446/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/amd64/default/libj9trc24. so
Java 5920 root mem REG 74373 878399/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/amd64/compressedrefs/libj9zlib24. so
Java 5920 root mem REG 205374 878432/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/amd64/default/libj9dmp24. so
Java 5920 root mem REG 35728 436315/usr/lib64/libnuma. so.1
Java 5920 root mem REG 15691 878377/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/amd64/compressedrefs/libhythr. so
Java 5920 root mem REG 46239 878376/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/amd64/compressedrefs/libhyprtshim24.so
Java 5920 root mem REG 6957 878403/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/amd64/compressedrefs/libvmi. so
Java 5920 root mem REG 47241 90327/lib64/librt-2.11.3.so
Java 5920 root mem REG 259675 878391/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/amd64/compressedrefs/libj9prt24. so
Java 5920 root mem REG 22489 878385/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/amd64/compressedrefs/libj9hookable24. so
Java 5920 root mem REG 70975 878394/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/amd64/compressedrefs/libj9thr24. so
Java 5920 root mem REG 712429 878448/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/amd64/default/libj9vm24. so
Java 5920 root mem REG 164469 878453/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/amd64/default/libjvm. so
Java 5920 root mem REG 86110 878311/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/bin/j9vm/libjvm. so
Java 5920 root mem REG 541821 90305/lib64/libm-2.11.3.so
Java 5920 root mem REG 1754140 90297/lib64/libc-2.11.3.so
Java 5920 root mem REG 19149 90303/lib64/libdl-2.11.3.so
Java 5920 root mem REG 42772 878460/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/amd64/jli/libjli. so
Java 5920 root mem REG 135690 90323/lib64/libpthread-2.11.3.so
Java 5920 root mem REG 151051 90290/lib64/ld-2.11.3.so
Java 5920 root mem REG 217016 2906402/var/run/nscd/passwd
Java 5920 root mem REG 26050 576479/usr/lib64/gconv/gconv-modules.cache
Java 5920 root mem REG 257156 576144/usr/lib/locale/zh_CN.utf8/LC_CTYPE
Java 5920 root mem REG 14684 878401/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/amd64/compressedrefs/libjsig. 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 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 10160 878493/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/annotation. jar
Java 5920 root 5r REG 189574 878494/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/beans. jar
Java 5920 root 6r REG 435869 878550/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/java. util. jar
Java 5920 root 7r REG 216671 878556/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/jndi. jar
Java 5920 root 8r REG 54586 878558/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/logging. jar
Java 5920 root 9r REG 287085 878567/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/security. jar
Java 5920 root 10r REG 128157 878569/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/SQL. jar
Java 5920 root 11r REG 1198418 878542/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/ibmorb. jar
Java 5920 root 12r REG 426065 878543/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/ibmorbapi. jar
Java 5920 root 13r REG 536255 878536/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/ibmcfw. jar
Java 5920 root 14r REG 20511988 878566/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/rt. jar
Java 5920 root 15r REG 9601775 878496/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/charsets. jar
Java 5920 root 16r REG 501898 878565/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/resources. jar
Java 5920 root 17r REG 1161371 878544/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/ibmpkcs. jar
Java 5920 root 18r REG 133770 878534/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/ibmcertpathfw. jar
Java 5920 root 19r REG 27626 878538/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/ibmjgssfw. jar
Java 5920 root 20r REG 60208 878540/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/ibmjssefw. jar
Java 5920 root 21r REG 10966 878545/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/ibmsaslfw. jar
Java 5920 root 22r REG 97697 878537/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/ibmjcefw. jar
Java 5920 root 23r REG 932373 878539/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/ibmjgssprovider. jar
Java 5920 root 24r REG 442208 878541/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/ibmjsseprovider2.jar
Java 5920 root 25r REG 357168 878535/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/ibmcertpathprovider. jar
Java 5920 root 26r REG 152611 878546/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/ibmxmlcrypto. jar
Java 5920 root 27r REG 375 878560/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/management-agent.jar
Java 5920 root 28r REG 8915002 878575/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/xml. jar
Java 5920 root 29r REG 91126 878555/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/jlm. jar
Java 5920 root 30r REG 363834 878551/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/javascript. jar
Java 5920 root 31r REG 240130 886599/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/ext/ibmjcefips. jar
Java 5920 root 32r REG 1273 486857/usr/local/test/socketClient. jar
Java 5920 root 33r REG 554639 886594/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/ext/dtfj. jar
Java 5920 root 34r REG 153450 886604/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/ext/ibmxmlencprovider. jar
Java 5920 root 35r REG 277912 886606/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/ext/jdmpview. jar
Java 5920 root 36r REG 481038 886589/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/ext/IBMKeyManagementServer. jar
Java 5920 root 37r REG 232590 886601/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/ext/ibmkeycert. jar
Java 5920 root 38r REG 183719 886588/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/ext/CmpCrmf. jar
Java 5920 root 39r REG 206636 886598/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/ext/ibmcmsprovider. jar
Java 5920 root 40r REG 17081 886593/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/ext/dtfj-interface.jar
Java 5920 root 41r REG 50129 886605/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/ext/jaccess. jar
Java 5920 root 42r REG 20228 886590/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/ext/IBMSecureRandom. jar
Java 5920 root 43r REG 23831 886608/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/ext/xmlencfw. jar
Java 5920 root 44r REG 353513 886602/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/ext/ibmpkcs11impl. jar
Java 5920 root 45r REG 1202552 886596/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/ext/gskikm. jar
Java 5920 root 46r REG 148480 886595/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/ext/dtfjview. jar
Java 5920 root 47r REG 8230 886592/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/ext/dnsns. jar
Java 5920 root 48r REG 112283 886597/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/ext/healthcenter. jar
Java 5920 root 49r REG 67395 886603/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/ext/ibmsaslprovider. jar
Java 5920 root 50r REG 992455 886600/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/ext/ibmjceprovider. jar
Java 5920 root 51r REG 67801 886591/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/ext/JavaDiagnosticsCollector. jar
Java 5920 root 52r REG 468772 886607/usr/lib64/jvm/java-1_6_0-ibm-1.6.0/jre/lib/ext/localedata. jar
Java 5920 root 53r REG 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 1565661 0t0 can't identify protocol
Java 5920 root 56u IPv6 1571368 0t0 TCP node1: 48708-> 192.168.22.15: irdmi (CLOSE_WAIT)


Node is TCP, which is the consumption of socket descriptors generated by two socketclients. the other 99 file descriptors are opened files related to socket functions. When the Socket is closed, the Socket descriptor of node TCP will not be recycled by the system. Therefore, if you forget to close the socket (Scoket) when using the socket, it will not only consume memory, but also consume the maximum number of files opened by the system, which will affect the normal operation of the application.

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.