连接到套接字会在TIME_WAIT状态下保留连接

时间:2016-09-04 12:57:00

标签: c sockets

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

以下代码运行正常。我遇到的问题是连接似乎没有正确关闭。我留下了一堆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

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

  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)));
  }

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

1 个答案:

答案 0 :(得分:0)

  

TCP要求关闭连接的端点进一步阻塞   连接在同一主机/端口对上,直到没有来自的数据包   该连接保留在网络中。

     

要临时阻止连接,一个端点会保留TCP的副本   控制块(TCB),表示连接已终止   最近。这种连接处于TIME-WAIT状态。连接在   TIME-WAIT被移至CLOSED并且足够后丢弃它们的TCB   时间已过,来自同一连接的所有数据包都已离开   网络。数据包通过到达其中一个网络离开网络   终点和被拒绝,或者到期时间到期   路由器上的(TTL)字段并被删除。

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