Improve Linux connection restrictions

Source: Internet
Author: User
Tags connection reset htons
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 */

# Define LPORT 3333
# Define LISTENQ 1024
# Define OPEN_MAX 50000
# Deprecision MAX_LINE 100
Void setnonblocking (int sock)
{
Int opts;
Opts = fcntl (sock, F_GETFL );
If (opts <0)
{
Perror ("fcntl (sock, GETFL )");
Exit (1 );
}

Opts = opts | O_NONBLOCK;
If (fcntl (sock, F_SETFL, opts) <0)
{
Perror ("fcntl (sock, SETFL, opts )");
Exit (1 );
}
}
Int main (int argc, char ** argv)
{
// Int I, maxi, listenfd, connfd, sockfd;
// Int nready; ssize_t n;


Int listenfd;
Struct sockaddr_in cliaddr; // sock description of the Client
Struct sockaddr_in servaddr; // sock description of the server


Struct pollfd * array_conn;
Array_conn = new pollfd [OPEN_MAX];

If (listenfd = socket (AF_INET, SOCK_STREAM, 0) =-1)
{
Perror ("socket create error! ");
Exit (1 );
}
Bzero (& servaddr, sizeof (servaddr ));
Servaddr. sin_family = AF_INET;
Servaddr. sin_addr.s_addr = htonl (INADDR_ANY );
Servaddr. sin_port = htons (LPORT );

/* Reset socket */
Int opt = 1;
If (setsockopt (listenfd, SOL_SOCKET, SO_REUSEADDR, (char *) & opt, sizeof (opt) <0)
{
Close (listenfd );
Perror ("! Cserver: init_comm () setsockopt error! ");
Exit (1 );
}

If (bind (listenfd, (struct sockaddr *) & servaddr, sizeof (servaddr) =-1)
{
Perror ("bind error! ");
Exit (1 );
}
Listen (listenfd, LISTENQ );

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 );

}
Delete [] sock;
Return 0;
}
Related Article

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.