Boost logo

Boost :

Subject: Re: [boost] Boost ASIO - Client Request TCP Help..!!
From: Joseph Van Riper (fleeb.fantastique_at_[hidden])
Date: 2013-10-14 07:09:30


On Mon, Oct 14, 2013 at 7:07 AM, Joseph Van Riper <
fleeb.fantastique_at_[hidden]> wrote:

> On Mon, Oct 14, 2013 at 2:32 AM, Rahul Mathur <srivmuk_at_[hidden]> wrote:
>
>> In continuation of above original code, as send() static API worked fine,
>> now to to set "Keep Alive Option" for the socket, I did tried as -
>>
>> ---
>> int sockKeepAliveOption = 1;
>> int n2 = setsockopt(socket.native(), SOL_SOCKET, SO_SNDTIMEO,
>> &sockKeepAliveOption, sizeof(sockKeepAliveOption));
>>
>> if(n2 == -1)
>> std::cout << "Setsocket option Err: SO_KEEPALIVE -- unable to set
>> keep alive tcp connection." << std::endl;
>> else
>> std::cout << "S0_KEEPALIVE set, with SOL_SOCKET --- Keep Alive
>> option set." << std::endl;
>> ---
>>
>> It goes to if condition of "n2 = -1", i.e unable to set keep alive tcp
>> connection.
>>
>> Please suggest - How can I correct above to have "Keep Alive option set".
>>
>> Thanks!!
>
>
> This page might be helpful:
>
>
> http://www.boost.org/doc/libs/1_54_0/doc/html/boost_asio/reference/basic_stream_socket/keep_alive.html
>
> So, I think you'd do something like:
>
> socket.set_option( boost::ip::tcp::socket::keep_alive( true ) );
>

Oh, if you want a version of this that doesn't throw an exception if the
option fails:

boost::system::error_code ec;
socket.set_option( boost::ip::tcp::socket::keep_alive( true ), ec );
if ( ec )
{
   std::cout << "socket's keep-alive option could not be set: " <<
ec.message() << std::endl;
}


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk