
On Mon, Oct 14, 2013 at 7:07 AM, Joseph Van Riper < fleeb.fantastique@gmail.com> wrote:
On Mon, Oct 14, 2013 at 2:32 AM, Rahul Mathur
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_str...
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; }