发送不感知套接字挂起错误

时间:2016-07-08 11:53:59

标签: sockets tcp

我的TCP客户端使用tcp_keepalive_interval = 10stcp_keepalive_time = 1stcp_keepalive_probes = 10实现了一个keepalive功能。

但是,在连接断开超过20秒后,send函数没有选择任何错误。理想情况下,启用keepalive后,应将20秒(keepalive_interval + keepalive_probes*keepalive_timeE_TIMEDOUT添加到套接字挂起错误中。

  

正如this回答所说,

     

" read(2)和write(2)都首先检索任何挂起的错误   甚至在尝试处理任何数据之前的套接字。"

当keepalive关闭连接时,send应选择E_TIMEDOUT或任何套接字错误,但不会在下面的代码中发生。

    int sockfd = -1;
    if ((sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) {
        cerr<<"ERROR: Failed to obtain Socket Descriptor!"<<endl;
        return -1;
    }

    //setting keepalive
    int optval;
    socklen_t optlen = sizeof(optval);

    //setting keepalive
    optval = 1;
    optlen = sizeof(optval);
    if(setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, &optval, optlen) < 0) {
        cerr<<"set keepalive failed"<<endl;
        return -1;
    }
    //setting tcp_keepalive_intvl
    optval = 10;
    optlen = sizeof(optval);
    if(setsockopt(sockfd, SOL_TCP, TCP_KEEPINTVL, &optval, optlen) < 0) {
        cerr<<"set tcp_keepalive_interval failed"<<endl;
        return -1;
    }
    //setting tcp_keepalive_time
    optval = 1;
    optlen = sizeof(optval);
    if(setsockopt(sockfd, SOL_TCP, TCP_KEEPIDLE, &optval, optlen) < 0) {
        cerr<<"set tcp_keepalive_time failed"<<endl;
        return -1;
    }
    //setting tcp_keepalive_probes
    optval = 10;
    optlen = sizeof(optval);
    if(setsockopt(sockfd, SOL_TCP, TCP_KEEPCNT, &optval, optlen) < 0) {
        cerr<<"set tcp_keepalive_probe failed"<<endl;
        return -1;
    }

    struct sockaddr_in remote_addr;
    remote_addr.sin_family = AF_INET;
    remote_addr.sin_port = htons(#port_no);
    remote_addr.sin_addr.s_addr = inet_addr(#ip_addr);
    memset(&remote_addr.sin_zero, 0, 8);
    if (connect(sockfd, (struct sockaddr *)&remote_addr, sizeof(struct sockaddr)) == -1) {
        cerr<<"Connect failed with ERRORNO "<<errno<<endl;
        return -1;
    } else {
        cout<<"TcpClient.cpp connected to server"<<endl;
    }
    while(1) {
        char data[20] = "hi hello";
        int ret = -1;
        if((ret = send(sockfd, data, 20, 0)) < 0) {
            cerr<<"TcpClient.cpp:- failed to send_data, ERROR CODE: "<<errno<<endl;
            return -1;
        } else if (ret == 0) {
            cout<<"send returns 0"<<endl;
        } else {
            cout<<"data sent"<<endl;
        }        
        sleep(1);
    }
    getchar();
    return 0;

我在带有gcc编译器的linux机器上测试了这段代码。 注意:我使用recv尝试了相同的代码,并且整齐地选择了E_TIMEDOUT错误。

1 个答案:

答案 0 :(得分:1)

你还没有等待足够长的时间。来自http://tldp.org/HOWTO/TCP-Keepalive-HOWTO/usingkeepalive.html

tcp_keepalive_time

    the interval between the last data packet sent (simple ACKs are not considered data) and the first keepalive probe; after the connection is marked to need keepalive, this counter is not used any further 

tcp_keepalive_intvl

    the interval between subsequential keepalive probes, regardless of what the connection has exchanged in the meantime 

tcp_keepalive_probes

    the number of unacknowledged probes to send before considering the connection dead and notifying the application layer 

因此,使用(10,10,10)的值,Keepalive将在从对等方收到最后一个数据后10秒才开始。然后需要在连接宣告死亡之前确认10个探测器,每个探测器间隔10秒钟。