Improve linux connection restrictions

xiaoxiao2021-03-06  41

General LINUX Limits Limit Limit in TD_SETSIZE, the system defaults to 1024, determined by fd_setsize. 1. Modify the method: Modify the /usr/tec/security/limits.conf file, add * Soft Nofile 20000 * Hard Nofile 20000 and then the Reboot System. The server can establish a connection to 20,000, and its connection method is to use Connect, Accept, and note that it is not possible with SELECT. 2. The above method is not available without the SELECT method. If you use SELECT, you can only open 1024, because the number of SELECT is determined by fd_setsize. Then we can use Poll to replace SELECT, and the Poll array size can be defined according to our own needs so that this problem is solved. 3. LINUX is to manage the system through a file mode, so the system can carry how much TCP connection and system file open number of capabilities are related. In addition, in / proc / sys / file-max, the number of files that can be opened up to the system can be opened. Access code: server side: 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 # define MAX_LINE 100void 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; // Client's SOCK Description Struct SockAddr_in Servaddr; // Server SOCK Description

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); / * set socket reuse * / 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 (Struct SockAddr *) & Servaddr, SIZEOF (SIZEOF (SIZEOF (SIZEOF (SIDDR)) == -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

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 maxi) Maxi = i; if (--NReady <= 0) Continue; / * no more readable descriptors * /}

/ * Check all clients for data * / char line [100]; int suckfd; for (i = 1; i <= maxi; i ) {/ * check all clients for data * / if ((sockfd = array_conn [i] .fd) <0) Continue; IF (array_conn [i] .revents & (pollrdnorm | pollerr)) {IF ((n = read (sockfd, line, max_line) <0) {if (errno == econnreset) { / * 4connection reset by client * / close (sockfd); array_conn [i] .fd = -1;} else {printf ("readline error / n");}}}} else if (n == 0) {/ * 4connection Closed by client * / close (sockfd); array_conn [i] .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_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 sock; address.sin_family = af_addr = inet_addr ("192.168.1.249"); address.sin_port = Htons (3333); address_len = sizeof (address); while (1) {sock = socket (AF_INET, SOCK_STREAM, 0); if (SSOCK <0) {Printf ("Local Sockfd Error / N"); Break;}

Connect_flag = Connect (SSOCK, STRUCKADDR *) & 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); // car RBUF [100]; // read (SSOCK, RBUF, 100); // Printf ("% D Receive from Server;% S / N", N, RBUF);} delete [] sock; return 0;}

转载请注明原文地址:https://www.9cbs.com/read-55198.html

New Post(0)