使用setrlimit修改进程打开的句柄数上限
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <string.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <netinet/in.h>
#include <stdio.h>
#include <string.h>
#include <sys/time.h>
#include <sys/resource.h>
int main(void)
{
int i = 0;
int socket_descriptor = -1;
rlimit ResLimit;
ResLimit.rlim_cur = SHRT_MAX;
ResLimit.rlim_max = SHRT_MAX;
setrlimit(RLIMIT_NOFILE, &ResLimit);
while (true)
{
socket_descriptor = socket(AF_INET, SOCK_STREAM, 0);
if (socket_descriptor == -1)
break;
++i;
}
printf("%d\n", i);
return 0;
}