如何确定网络堆栈何时准备好再次打开到同一主机/端口的套接字?

How can I determine when the network stack is ready to open socket to same host/port again?

本文关键字:主机 套接字 堆栈 网络 何确定 何时 准备好      更新时间:2023-10-16

我有以下 C 程序,其主要功能runTCPConnectivityCheck打开一个 TCP 套接字,向其发送一串字节并关闭它:

#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
static int runTCPConnectivityCheck(const int port)
{
int fd = socket(AF_INET, SOCK_STREAM, 0);
if (fd < 0)
{
printf("socket failedn");
return 1;
}
int reuseAddrTrue = 1;
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuseAddrTrue, sizeof(reuseAddrTrue)) != 0)
{
printf("setsockopt failedn");
return 1;
}
struct sockaddr_in serv_addr = { 0 };
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(port);
if (inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr) != 1)
{
printf("inet_pton failedn");
return 1;
}
if (connect(fd, (struct sockaddr*)(&serv_addr), sizeof(serv_addr)) != 0)
{
printf("connect failedn");
return 1;
}
const char* message = "hello";
const size_t messageLength = strlen(message);
if (send(fd, message, messageLength, 0) != messageLength)
{
printf("send failedn");
return 1;
}
shutdown(fd, SHUT_RDWR);
close(fd);
return 0;
}
int main()
{
for (int i = 0; i < 10; ++i)
{
printf("%sn", runTCPConnectivityCheck(5555) == 0 ? "success" : "failure");
//sleep(1);
}
}

如果我尝试重复测试同一端口,则对runTCPConnectivityCheck的第一次调用工作正常,但所有后续调用在调用connect时都失败。如果我添加调用以sleep,那么它也有效。据推测,1 秒的延迟使网络堆栈有足够的时间来清理套接字,以便我可以再次连接到同一端口。

首先:我是否正确关闭了插座?其次:如果我正确关闭它,确定网络堆栈何时准备好再次连接到同一主机和端口(而不是我当前使用的蹩脚sleep)的标准方法是什么?

更新

更多详细信息:connect失败,错误代码为 61 或ECONNREFUSED,而strerror返回"连接被拒绝"。

在这种情况下,服务器是与测试程序在同一台计算机上循环运行的netstat/nc命令:

#!/bin/bash
while true; do
nc -l 5555
done

程序现在是C,真的。

普遍的共识是客户端代码没有什么特别的问题。事实证明,我的测试服务器无法跟上我的客户端:

#!/bin/bash
while true; do
nc -l 5555
done

我用Python编写了一个分叉服务器,它能够毫无困难地为所有客户端连接提供服务。