据我所知,'time_wait' stat 中的 tcp 端口无法使用。但是,在我的实验中,服务器重用了“time_wait”端口?为什么?
首先,在客户端机器中,输入 command ehco 40000 40001 > /proc/sys/net/ipv4/ip_local_port_range
。因此,TCP 端口的最大数量为 2。
服务器代码
while (1) {
int len = sizeof(struct sockaddr);
fd = accept(sfd, &remote, &len);
read(fd, buf, sizeof(buf));
close(fd);
}
Run Code Online (Sandbox Code Playgroud)
客户代码
for (i = 0; i < 3; i++)
{
sleep(1);
pid_t pid = fork();
if (pid == 0)
{
handler();
exit(0);
}
}
void handler()
{
* ............. */
res = connect(sfd, result->ai_addr, result->ai_addrlen);
if (res == -1) {
perror("error");
exit(1);
}
printf("connect\n");
}
Run Code Online (Sandbox Code Playgroud)
展示 …