C同时连接1M客户端

时间:2015-09-23 11:15:38

标签: c sockets connection limit

我在Red Hat 7服务器上,我想让一台C服务器同时连接1M个客户端。

ulimit -n输出为1000000

sudo vi /etc/security/limits.conf

最后包含

*         hard    nofile      1000000
*         soft    nofile      1000000
root      hard    nofile      1000000
root      soft    nofile      1000000


sudo vi /etc/sysctl.conf

在某些时候包含

fs.file-max = 90000000000000

当我不运行我的C服务器时,我的输出

cat /proc/sys/fs/file-nr

5184    0   90000000000000

当我运行我的C服务器时,我的

输出
cat /proc/sys/fs/file-nr

116537  0   90000000000000


我可以连接大约55000个客户端,因为我在同一台机器上运行服务器和客户端,它意味着2个文件描述符foreach客户端,所以...我有这个号码116537.但为什么不更多,当我提出每个系统限制我发现关于那与我的问题有关?

这是服务器

#include <stdio.h>
#include <stdlib.h>
#include <netdb.h>
#include <netinet/in.h>
#include <string.h>

int main( int argc, char *argv[] )
{
   int sockfd, newsockfd, portno, clilen;
   char buffer[256];
   struct sockaddr_in serv_addr, cli_addr;
   int  n, pid;
   int son=0;   

   /* First call to socket() function */
   sockfd = socket(AF_INET, SOCK_STREAM, 0);

   if (sockfd < 0)
      {
      perror("ERROR opening socket");
      exit(1);
      }

   /* Initialize socket structure */
   bzero((char *) &serv_addr, sizeof(serv_addr));
   portno = 5001;

   serv_addr.sin_family = AF_INET;
   serv_addr.sin_addr.s_addr = INADDR_ANY;
   serv_addr.sin_port = htons(portno);

   /* Now bind the host address using bind() call.*/
   if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
      {
      perror("ERROR on binding");
      exit(1);
      }

   /* Now start listening for the clients, here
 *    * process will go in sleep mode and will wait
 *       * for the incoming connection
 *          */

   listen(sockfd, SOMAXCONN);
   clilen = sizeof(cli_addr);

   while (1)
   {
      newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
      if (newsockfd < 0)
         {
         perror("ERROR on accept");
         exit(1);
         }

      son++; 
      if(son % 1000 == 0)     
        printf("client nr %d connected with fd %d\n", son, newsockfd);

   } /* end of while */
}

客户

#include <stdio.h>
#include <stdlib.h>
#include <netdb.h>
#include <netinet/in.h>
#include <string.h>

int main(int argc, char *argv[])
{
   int sockfd, portno, n;
   struct sockaddr_in serv_addr;
   struct hostent *server;

   char buffer[256];

   if (argc <3) {
      fprintf(stderr,"usage %s hostname port\n", argv[0]);
      exit(0);
   }
   portno = atoi(argv[2]);
   server = gethostbyname(argv[1]);

   if (server == NULL) {
      fprintf(stderr,"ERROR, no such host\n");
      exit(0);
   }

   bzero((char *) &serv_addr, sizeof(serv_addr));
   serv_addr.sin_family = AF_INET;
   bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr, server->h_length);
   serv_addr.sin_port = htons(portno);

    int ionut; 
   for(ionut=0; ionut<90000; ionut++) { 
     /* Create a socket point */
     sockfd = socket(AF_INET, SOCK_STREAM, 0);
     if(ionut % 1000 == 0) { 
       printf("connected 1000 sockets\n");   
      // sleep(1);
     }
     if (sockfd < 0)
     {
        perror("ERROR opening socket");
        exit(1);
     }

    /* Now connect to the server */
     if (connect(sockfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0)
     {
        perror("ERROR connecting");
        exit(1);
     }

   }
   return 0;
}

0 个答案:

没有答案