gpt4 book ai didi

连接到套接字使连接处于 TIME_WAIT 状态

转载 作者:太空宇宙 更新时间:2023-11-04 04:28:09 27 4
gpt4 key购买 nike

我正在尝试解析主机名,然后打开/关闭与主机的套接字。

下面的代码工作正常。我遇到的问题是连接似乎没有正确关闭。我只剩下一堆 TIME_WAITS:

tcp        0      0 192.168.142.139:44475   172.217.23.14:443       TIME_WAIT
tcp 0 0 192.168.142.139:44362 45.79.5.162:80 TIME_WAIT
tcp 0 0 192.168.142.139:44373 45.79.5.162:80 TIME_WAIT
tcp 0 0 192.168.142.139:44461 172.217.23.14:443 TIME_WAIT
tcp 0 0 192.168.142.139:44468 172.217.23.14:443 TIME_WAIT
tcp 0 0 192.168.142.139:44472 172.217.23.14:443 TIME_WAIT
tcp 0 0 192.168.142.139:44474 172.217.23.14:443 TIME_WAIT
tcp 0 0 192.168.142.139:44459 172.217.23.14:443 TIME_WAIT
tcp 0 0 192.168.142.139:44470 172.217.23.14:443 TIME_WAIT
tcp 0 0 192.168.142.139:44463 172.217.23.14:443 TIME_WAIT
tcp 0 0 192.168.142.139:44464 172.217.23.14:443 TIME_WAIT

我并不特别需要向主机发送任何特定内容,它更像是一个一般的互联网检查。我也尝试过使用非阻塞连接,然后是选择来进行相同的尝试。相同的结果。

  int port = 443;
char *hostname = "google.com";

int open_socket(char *ip)
{
int error = 0; // Socket error
struct sockaddr_in address;
short int sock = -1;
fd_set fdset;
struct timeval tv;
int so_keepalive = 0;

sock = socket(PF_INET, SOCK_STREAM , 0);
if (sock < 0)
return 150;

address.sin_family = AF_INET;
address.sin_addr.s_addr = inet_addr(ip);
address.sin_port = htons(port);
/* address.sin_addr.s_addr = INADDR_ANY; */

FD_ZERO(&fdset);
FD_SET(sock, &fdset);
tv.tv_sec = 3;
tv.tv_usec = 0;

int yes = 1;
// setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(int));
// setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(struct timeval));
setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, (char *)&tv, sizeof(struct timeval));
setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, &so_keepalive, sizeof(so_keepalive));

if (connect(sock, (struct sockaddr *)&address , sizeof(address)) < 0)
error = 150;

if (error == 0) {
char *message = "HELLO";
if (send(sock , message , strlen(message) , 0) < 0)
error = 180;

char server_reply[2000];
if( recv(sock, server_reply , 2000 , 0) < 0)
error = 190;
}

/* shutdown(sock, SHUT_RDWR); */
close(sock);
return error;
}

int connection_check()
{
struct addrinfo *result;
struct in_addr addr;

int error;

error = getaddrinfo(hostname, NULL, NULL, &result);
if (error != 0)
{
fprintf(stderr, "DNS Lookup Failed: %s\n", gai_strerror(error));
return 100;
}

addr.s_addr = ((struct sockaddr_in *)(result->ai_addr))->sin_addr.s_addr;
printf("\nUsing %s for internet check\n", inet_ntoa(addr));
freeaddrinfo(result);
return(open_socket(inet_ntoa(addr)));
}

有人可以建议我应该如何正确处理这个问题。

最佳答案

TCP requires that the endpoint that closes a connection blocks further connections on the same host/port pair until there are no packets from that connection remaining in the network.

To temporarily block connections, one endpoint keeps a copy of the TCP control block (TCB) indicating that the connection has been terminated recently. Such a connection is in the TIME-WAIT state. Connections in TIME-WAIT are moved to CLOSED and their TCB discarded after enough time has passed that all packets from the same connection have left the network. Packets leave the network by arrive at one of the endpoints and being rejected, or arriving with an expired time-to-live (TTL) field at a router and being deleted.

http://www.isi.edu/touch/pubs/infocomm99/infocomm99-web/

关于连接到套接字使连接处于 TIME_WAIT 状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39317137/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com