日期:2014-05-16  浏览次数:20760 次

当TCP缓冲区有数据时断开连接,向对端发送的是RST而不是FIN?
RT。。比如服务端向客户端发送数据后就read套接字,客户端睡眠几秒后(保证服务端的数据已经到缓冲区了)就直接退出,然后服务端的read返回-1,错误信息是 Connection reset by peer。用TCPDUMP观察,发现如果服务端没有发送数据,客户端退出时就发送FIN,否则就发送RST。。请问各位,TCP为什么要这么设计,基于什么考量阿?

------解决方案--------------------
本帖最后由 mymtom 于 2013-08-13 16:09:02 编辑
客户端设置了linger选项,并且将linger时间设置为0

/*-
 * vi:set ts=4 sw=4:
 */
#ifndef lint
static const char rcsid[] = "$Id$";
#endif /* not lint */

/**
 * @file        clnt.c
 * @brief
 */

#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netdb.h>

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char *argv[])
{
        int fd;
        int ret;
        struct sockaddr_in addr;
        struct linger linger;
        char buf[] = "2013-08-13 14:29:53";

        memset(&addr, 0, sizeof(addr));
        addr.sin_family = AF_INET;
        addr.sin_addr.s_addr = inet_addr("127.0.0.1");
        addr.sin_port = htons(12345);

        fd = socket(PF_INET, SOCK_STREAM, 0);

        if (argc == 2 && atoi(argv[1]) > 0) {
                linger.l_onoff = 1;
                linger.l_linger = 0;