首页 文章

为什么RST数据包不需要TIME_WAIT状态?

提问于
浏览
2

我知道 TIME_WAIT 是为了防止一个连接的延迟段被误解为后续连接的一部分 . 在连接处于TIME_WAIT等待状态时到达的任何段都将被丢弃 .

在我的实验中,当客户端发送RST数据包而不是FIN数据包时,我看不到 TIME_WAIT . 为什么?

Server

while (1) {
    int len = sizeof(struct sockaddr);
    fd = accept(sfd, &remote, &len);

    read(fd, buf, sizeof(buf));

    strcpy(buf, "Hello Client");
    write(fd, buf, strlen(buf));

    close(fd);
}

Client

res = connect(sfd, result->ai_addr, result->ai_addrlen);

strcpy(buf, "Hello Server!");
write(sfd, buf, strlen(buf));

close(sfd);

NOTE: 客户端发送RST而不是FIN,因为在关闭套接字之前它没有读取服务器已经发送的缓冲数据 .

2 回答

  • 0

    因为它声明没有这种联系,其效果是在没有偏见的情况下终止它 .

    换句话说,因为RFC 793明确表示在 RST 接收时不会发送任何响应,您必须进入 CLOSED 状态(除非在某些情况下与连接 Build 有关,在这种情况下您再次进入 LISTEN 状态) .

  • 1

    当您接收数据的挂起连接时,连接中断,因为您没有读取所有挂起的数据(可能在缓冲区中,已确认,未确认或仅在传输中)您正在破坏状态机,这是是什么引发 RST (从主机发送到另一端,响应到达连接这一侧的任何数据段) . 如果按照建议读取RFC文档,则连接处于错误状态,并且会向它接收的每个数据包回复 RST 帧...它不再是 TIME_WAIT 状态 .

    在读取 EOF 之前调用 close(2) (当你已从另一端收到 FIN 时,解锁0字节)条件 is a protocol error ,因为接收方(你)正在丢失传输到你身边的剩余数据 . 你有一个系统调用 shutdown(2) ,目的是表明你没有写更多数据的意图(一半关闭你的发送方),并允许等待剩余的数据来,它迫使你的一方发送 FIN 到另一端并将连接置于 FIN_WAIT1 状态(从另一侧等待 FIN 和/或 FIN&ACKACK

    注意

    TIME_WAIT state是一种状态,用于确保任何 in-transit 数据包有足够的时间到达目的地并正确处理 . 由于连接失败了两端 unsynchronized ,等待任何数据包到达没有任何意义,因为它们是 cannot be correctly processed . 没有数据包被发送以响应 RST 并且连接通常进入 CLOSED 状态 .

    RFC-793具体说:

    Sec 3.4 Establishing a connection

    [...]

    Reset Processing
    
    In all states except SYN-SENT, all reset (RST) segments are validated
    by checking their SEQ-fields.  A reset is valid if its sequence number
    is in the window.  In the SYN-SENT state (a RST received in response
    to an initial SYN), the RST is acceptable if the ACK field
    acknowledges the SYN.
    
    The receiver of a RST first validates it, then changes state.  If the
    receiver was in the LISTEN state, it ignores it.  If the receiver was
    in SYN-RECEIVED state and had previously been in the LISTEN state,
    then the receiver returns to the LISTEN state, otherwise the receiver
    aborts the connection and goes to the CLOSED state.  If the receiver
    was in any other state, it aborts the connection and advises the user
    and goes to the CLOSED state.
    

    所以,你可以在任何情况下阅读 TIME_WAITTIME_WAIT 状态 .

相关问题