how to set TCP keepalive?

545 views
Skip to first unread message

Bill Yan

unread,
Sep 17, 2014, 6:47:07 PM9/17/14
to native-cli...@googlegroups.com
hello,

we are porting our linux program to native client. we need to set keepalive for a tcp socket. on linux, the following code works:


    int32_t optval;
    socklen_t optlen
= sizeof(optval);
    optval
= 1;
    setsockopt
(sockfd, SOL_SOCKET, SO_KEEPALIVE, &optval, optlen);
#ifndef NACL_PNACL
   
const uint32_t keepaliveProbeCount = 20;
    optval
= keepaliveIntervalSec;
    setsockopt
(sockfd,  SOL_TCP, TCP_KEEPIDLE, &optval, optlen);
    setsockopt
(sockfd,  SOL_TCP, TCP_KEEPINTVL, &optval, optlen);
    optval
= keepaliveProbeCount;
    setsockopt
(sockfd,  SOL_TCP, TCP_KEEPCNT, &optval, optlen);
#endif

#endif


with native client however, it complains that TCP_KEEPIDLE, TCP_KEEPINTVL TCP_KEEPCNT, SOL_TCP are undefined. 

I guess the reason is because native client is using newlib, instead of glibc.

I tried to find an example of setting keep alive everywhere, but couldn't.

I tried to hardcode TCP_KEEPIDLE myself and use IPPROTO_TCP. doesn't work either.

I tried the BSD (mac os) method:
    setsockopt(sockfd, SOL_SOCKET,  SO_KEEPALIVE, &on, sizeof(on));
    setsockopt
(sockfd, IPPROTO_TCP, TCP_KEEPALIVE, &secs, sizeof(secs));


doesn't work too.

could you please show me a working example?

Sam Clegg

unread,
Sep 17, 2014, 8:16:28 PM9/17/14
to native-cli...@googlegroups.com
Hi bill,

The socket API the chrome exposes currently does support keepalive
option. The current options are documented here:
https://developer.chrome.com/native-client/pepper_dev/c/group___enums#ga1557c0bbce8739a3418e6027a9c44e12.

While we could add the missing constants the headers, and your code
would then compile, setting these options at runtime is currently not
possible.

cheers,
sam
> --
> You received this message because you are subscribed to the Google Groups
> "Native-Client-Discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to native-client-di...@googlegroups.com.
> To post to this group, send email to native-cli...@googlegroups.com.
> Visit this group at http://groups.google.com/group/native-client-discuss.
> For more options, visit https://groups.google.com/d/optout.

Biba Biba

unread,
Sep 18, 2014, 4:23:42 PM9/18/14
to Sam Clegg, native-cli...@googlegroups.com
Biba Biba liked your message with Boxer for Android.

18.09.2014 6:16 пользователь Sam Clegg <s...@chromium.org> написал:

Reply all
Reply to author
Forward
0 new messages