High concurrency network programming under Linux

Source: Internet
Author: User
Tags unix domain socket

  Linux under high concurrency Network programming 1, modify the user process can open the number of files limit on the Linux platform, regardless of writing the client program or server-side program, in the high-concurrency TCP connection processing, the highest concurrent number of the system to the user single process can open the number of files limit ( This is because the system creates a socket handle for each TCP connection, and each socket handle is also a file handle. You can use the Ulimit command to view the number of files that the system allows the current user process to open:    [[email protected] ~]$ ulimit-n1024 This means that each process of the current user is allowed to open up to 1024 files at a time. These 1024 files also have to remove each process must open the standard input, standard output, standard error, server listening socket, inter-Process communication UNIX domain socket and other files, then the remaining number of files available to the client socket connection is only about 1024-10 = About 1014. In other words, Linux-based communication programs allow up to 1014 simultaneous TCP connections by default. For a communication handler that wants to support a higher number of TCP concurrent connections, you must modify the soft limit (soft limit) and the hard limit (hardlimit) of the number of files that Linux has open simultaneously for the current user's process. The soft limit refers to the Linux in the current system can withstand the extent to further limit the number of files opened by the user at the same time; hard limits are the number of files that can be opened at the same time based on the system's Hardware resource status (mainly system memory). The soft limit is usually less than or equal to the hard limit.   The simplest way to modify the above limitation is to use the ulimit command: [[[email protected] ~]$ ulimit-n     The above command, specify the maximum number of files to be set for a single process to be allowed in. If the system echoes similar to "Operation Notpermitted", the above limitation modification fails, in effect because the value specified in is more than the soft or hard limit of the number of open files that the Linux system has on the user. Therefore, it is necessary to modify the Linux system's soft and hard limits on the number of open files to the user.   First step, modify the/etc/security/limits.conf file and add the following line to the file: Speng soft nofile 10240speng hard Nofile 10240 where Speng specifies the limit on the number of open files to modify for which user, the ' * ' is used to modify the limits of all users; soft or haRD Specifies whether to modify the soft or hard limit, and 10240 specifies the new limit value that you want to modify, that is, the maximum number of open files (note that the soft limit value is less than or equal to the hard limit). Save the file when you are finished modifying it.   Second step, modify the/etc/pam.d/login file to add the following line to the file: Session required/lib/security/pam_limits.so     This is to tell Linux that after the user completes the system login, the Pam_limits.so module should be called to set the system's maximum limit on the number of resources that the user can use, including the maximum number of files a user can open, and the pam_limits.so module from/etc/ The security/limits.conf file reads the configuration to set these throttling values. Save this file when you are finished modifying it.   Third, check the maximum number of open files at Linux system level, using the following command: [[email protected] ~]$ cat/proc/sys/fs/ file-max12158 This indicates that the Linux system is allowed to open at most (that is, the total number of open files for all users) 12,158 files, is the Linux system-level hard limit, all user-level open files limit should not exceed this number. Typically, this system-level hard limit is the best maximum number of simultaneous open file limits that Linux systems will calculate at startup, based on the state of the system's hardware resources, and should not be modified unless you want to set a value that exceeds this limit for the user-level open files limit.   The way to modify this hard limit is to modify the/etc/rc.local script to add the following line to the script: Echo 22158 >/proc/sys/fs/ File-max This is to allow Linux to forcibly set the number of system-level open files to 22158 after the boot is complete. Save this file when you are finished modifying it.   After completing the above steps, the system can be restarted, and it is generally possible to set the maximum number of files allowed to open simultaneously for a single process of the Linux system to a specified user. If you use the Ulimit-n command to view the number of files that the user can open after restarting, the limit is still lower than the maximum value set in the previous steps, possibly because using the ulimit-n command in User logon script/etc/profile limits the number of files that the user can open simultaneously. Because modifying the system through Ulimit-n can limit the maximum number of files that a user can open at the same time, the newly modified value is only less than or equal to the value of the last Ulimit-n setting, so it is not possible to use this command to increase the limit value. So, if any of these problems exist, you can only open/etc/prof.Ile script file, find in the file whether to use Ulimit-n limit the maximum number of files the user can open at the same time, if found, delete this line of command, or set its value to the appropriate value, and then save the file, the user exits and re-login to the system.     Through the above steps, the system limits on the number of open files are lifted for communication handlers that support high concurrent TCP connection processing. 2. Modify the network kernel restrictions on TCP connections when writing client-side communication handlers that support high concurrent TCP connections on Linux, it is sometimes found that although the system has lifted the limit on the number of simultaneous open files to the user, the number of concurrent TCP connections will still increase to a certain number. The phenomenon of new TCP connections cannot be successfully established. There are many reasons for this now.   The first reason may be because the Linux network kernel has a limit on the local port number range. At this point, further analysis of why the TCP connection could not be established, the problem will be found in the Connect () Call return failure, the view system error message is "Can ' t assign Requestedaddress". Also, if you use the Tcpdump tool to monitor the network at this time, you will find that the client sends a SYN packet of network traffic when there is no TCP connection at all. These conditions indicate a limitation in the local Linux system kernel. In fact, the root cause of the problem is that the TCP/IP Protocol implementation module of the Linux kernel restricts the range of local port numbers that correspond to all client TCP connections in the system (for example, the kernel restricts the range of local port numbers to 1024~32768). When a certain moment in the system

Programming high concurrency network under Linux

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.