#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
int main() {
int port;
// int maxport = 10000000;
int maxport = 65535;
int count = 0;
for (port = maxport; port > -1; port--) {
int sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0) continue;
const int on = 1;
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) continue;
struct sockaddr_in serv_addr;
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(port);
if (bind(sock, (struct sockaddr *)&serv_addr, sizeof(struct sockaddr_in)) < 0) continue;
count++;
close(sock);
}
printf("count = %d\n", count);
}
При maxport = 65535, count = 64494,
при maxport = 1000000, count = 37494.
Почему второе значение меньше? Вроде count должен быть больше?