Socket for Linux network programming (12) example of concurrent restriction of select function and application of poll function

Source: Internet
Author: User

The concurrent server, which can be achieved with select, is limited by two aspects.

1, a process can open the maximum file descriptor restrictions. This can be done by adjusting the kernel parameters. You can adjust or use the Setrlimit function setting by ulimit-n, but the maximum number that a system can open is also limited, which is related to memory size and can be viewed by Cat/proc/sys/fs/file-max

2, the Fd_set set capacity limit in the Select (Fd_setsize, typically 1024), which requires recompiling the kernel.

You can write a test program that only establishes a connection to see how many connections can be established, and the client program is as follows:

 #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/ inet.h> #include <signal.h> #include <stdlib.h> #include <stdio.h> #include <errno.h> #inc Lude <string.h> #define ERR_EXIT (m) \ do \ {\ perror (m); \ Exi T (exit_failure);
    \} while (0) int main (void) {int count = 0;
        while (1) {int sock;
            if ((sock = socket (pf_inet, Sock_stream, ipproto_tcp)) < 0) {sleep (4);
        Err_exit ("socket");
        } struct sockaddr_in servaddr;
        memset (&servaddr, 0, sizeof (SERVADDR));
        servaddr.sin_family = af_inet;
        Servaddr.sin_port = htons (5188);
    
        SERVADDR.SIN_ADDR.S_ADDR = inet_addr ("127.0.0.1");
    
        if (Connect (sock, (struct sockaddr *) &servaddr, sizeof (SERVADDR)) < 0) Err_exit ("Connect"); structSockaddr_in localaddr;
        socklen_t Addrlen = sizeof (LOCALADDR);
    
        if (getsockname (sock, struct sockaddr *) &localaddr, &addrlen) < 0) Err_exit ("GetSockName");
        printf ("ip=%s port=%d\n", Inet_ntoa (LOCALADDR.SIN_ADDR), Ntohs (Localaddr.sin_port));
    
    printf ("Count =%d\n", ++count);
return 0; }

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.