最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

tcp - How to set KEEP_ALIVE parameters via C code - Stack Overflow

programmeradmin1浏览0评论

I have a Client-Server set up in C. The Client is running on a development board (FreeRTOS with lwip stack). Server is running on a Ubuntu. I am trying to set the KEEP_ALIVE params using the following code:

int optval = 1;
ret =setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, &optval, sizeof(optval));
if (ret)
{
    printf("Failed to set socket option - SO_KEEPALIVE ret=%d, ret\n");
}
else
{
    printf("Keep Alive Set!\n");
}

ret = setsockopt(sock, IPPROTO_TCP, TCP_KEEPIDLE, &KEEP_ALIVE_TIME, sizeof(KEEP_ALIVE_TIME));
if (ret)
{
    printf("Failed to set socket option - TCP_KEEPIDLE ret=%d, ret\n");
}
else
{
    printf("Keep Idle Set!\n");
}

ret = setsockopt(sock, IPPROTO_TCP, TCP_KEEPINTVL, &KEEP_ALIVE_INTERVAL, sizeof(KEEP_ALIVE_INTERVAL));
if (ret)
{
    printf("Failed to set socket option - TCP_KEEPINTVL ret=%d, ret\n");
}
else
{
    printf("Keep Alive Interval Set!\n");
}

ret = setsockopt(sock, IPPROTO_TCP, TCP_KEEPCNT, &KEEP_ALIVE_PROBES, sizeof(KEEP_ALIVE_PROBES));
if (ret)
{
    printf("Failed to set socket option - TCP_KEEPCNT ret=%d, ret\n");
}
else
{
    printf("Keep Count Set!\n");
}

The SO_KEEPALIVE works fine and gets set but for the rest I keep getting a return value error code of 16. When I look this error code up it says the following:

16 EBUSY All Listen has already been called for this socket. Device or file to be accessed is busy. Check if the device or file is in use.

In my google trawling I have come across some posts that seem to suggest KEEP_ALIVE setting is not supported in the lwip stack. Some others seem to indicate this can be done by editing the lwipopts.h file.

Any help would be great.

发布评论

评论列表(0)

  1. 暂无评论