Improve Linux connection restrictions-Linux Enterprise Application-Linux server application information. For more information, see the following section. In linux, TCP connections are limited to TD_SETSIZE. The default value is 1024, which is determined by FD_SETSIZE.
1. modification method:
Modify the/usr/etc/security/limits. conf file and add
* Soft nofile 20000
* Hard nofile 20000
Then reboot the system.
The server can establish connections to 20000. The connection method is to directly use connect and accept. Note that select is not supported here.
2. The above method is not available without the select method. If you use select, you can only open 1024. This is because the number of select statements is determined by FD_SETSIZE. Then we can use poll instead of select. The poll array size can be defined based on our own needs, which solves this problem.
3. in linux, the system is managed through files. Therefore, the number of TCP connections that the system can hold and the number of system files opened are related.
In addition, the maximum number of files that can be opened by the system is defined in/proc/sys/file-max.
Code:
Server: pollserver. cpp
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
// # Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include /* For OPEN_MAX */
Array_conn [0]. fd = listenfd;
Array_conn [0]. events = POLLRDNORM;
Int I;
For (I = 1; I <OPEN_MAX; I ++) array_conn
. Fd =-1;/*-1 indicates available entry */ Int maxi = 0;
Int connfd; Int nready; Int clilen; Size_t n; Int x; x = 0; For (;;){ Int nready = poll (array_conn, maxi + 1, 0 );
If (array_conn [0]. revents & POLLRDNORM) { Clilen = sizeof (cliaddr ); Connfd = accept (listenfd, (struct sockaddr *) & cliaddr, (socklen_t *) & clilen ); Printf ("| % d: Connection from % s: % d \ n", x ++, inet_ntoa (cliaddr. sin_addr), cliaddr. sin_port ); // Setnonblocking (connfd ); For (I = 1; I <OPEN_MAX; I ++) If (array_conn. Fd <0) { Array_conn. Fd = connfd; // save descriptor Break; } If (I = OPEN_MAX) { Printf ("too multicast clients "); Break; } Array_conn. Events = POLLRDNORM; If (I> maxi) maxi = I; If (-- nready <= 0) continue;/* no more readable descriptors */ }
/* Check all clients for data */ Char line [100]; Int sockfd; For (I = 1; I <= maxi; I ++) {/* Check all clients for data */ If (sockfd = array_conn. Fd) <0) continue; If (array_conn. Revents & (POLLRDNORM | POLLERR )) { If (n = read (sockfd, line, MAX_LINE) <0) { If (errno = ECONNRESET) { /* 4 connection reset by client */ Close (sockfd ); Array_conn. Fd =-1; } Else { Printf ("readline error \ n "); } } Else if (n = 0) { /* 4 connection closed by client */ Close (sockfd ); Array_conn. Fd =-1; } Else { Char sb [10]; Printf ("Recv: % s from socket % d", line, sockfd ); Sprintf (sb, "Pong! "); Write (sockfd, sb, 10 ); } If (-- nready <= 0) Break;/* no more readable descriptors */ } }
} } Client pollclient. cpp # Include # Include # Include # Include # Include # Include
Int main () { Int sockfd; Int address_len; Int connect_flag; Struct sockaddr_in address; Int connect_result; Char client_ch, server_ch;
Int * sock; Sock = new int [50000]; Int index = 0; Int n = 0; Int ssock; Address. sin_family = AF_INET; Address. sin_addr.s_addr = inet_addr ("192.168.1.249 "); Address. sin_port = htons (3333 ); Address_len = sizeof (address ); While (1) { Ssock = socket (AF_INET, SOCK_STREAM, 0 ); If (ssock <0) { Printf ("local sockfd error \ n "); Break; }
Connect_flag = connect (ssock, (struct sockaddr *) & address, address_len ); If (connect_flag =-1) { Perror ("client "); Break; } Printf ("% d Connected! ", N ++ ); Char cch [10]; Sprintf (cch, "Ping *"); Write (ssock, cch, 10 ); Char rbuf [100]; Read (ssock, rbuf, 100 ); Printf ("% d Receive from server; % s \ n", n, rbuf ); // Char c_ch; // C_ch = '*'; // Write (ssock, & c_ch, 1 ); // Char rbuf [100]; // Read (ssock, rbuf, 100 ); // Printf ("% d Receive from server; % s \ n", n, rbuf );
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.