Tcp keep-alive

hello i want using keep-alive in c language application.
so i use this functions in my c language application.

int EnableKeepalive(int sockfd, char keepalive, char seconds, char count, char interval)
{
    int ret;
    ret = setsockopt(sockfd,SOL_SOCKET,SO_KEEPALIVE, (void *)&keepalive,sizeof(keepalive));

    if (ret < 0)
    {
		perror("setsockopt SO_KEEPALIVE");
        // fprintf(stderr, "setsockopt error: %s\n", strerror(errno)); 
        printf("err keepalive0\n");
        // return -1;
    }

    ret = setsockopt(sockfd, IPPROTO_TCP, TCP_KEEPIDLE, (void *)&seconds, sizeof(seconds));

    if (ret < 0)
    {
		perror("setsockopt TCP_KEEPIDLE");
        printf("err keepalive1\n");
        // return -1;
    }

    ret = setsockopt(sockfd, IPPROTO_TCP, TCP_KEEPCNT, (void *)&count, sizeof(count));

    if (ret < 0)
    {
		perror("setsockopt TCP_KEEPCNT");
        printf("err keepalive2\n");
        // return -1;
    }

    ret = setsockopt(sockfd, IPPROTO_TCP, TCP_KEEPINTVL, (void *)&interval, sizeof(interval));

    if (ret < 0)
    {
		perror("setsockopt TCP_KEEPINTVL");
        printf("err keepalive3\n");
        // return -1;
    }
}

but setsockopt() return err like this


in openwrt need packages or linux kernel build options to using keep-alive ?

Please post

ubus call system board

Does it work on any Linux or Windows? Frankly looks like teacher asked you to fix it for grades

this is a homework, right?

please read man socket 7
"SO_KEEPALIVE
Enable sending of keep-alive messages on connection-oriented sockets. Expects an integer boolean flag."

1 Like

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.