Boost ASIO - Client Request TCP Help..!!

All,
Below is the client request code which makes a CONNECT request to remote
SERVER (having IP & PORT#) but gives error code during send() process -
---
#include <iostream>
#include <string>
#include

As the compiler is telling you, you can't create a buffer object with just a PacketFormat pointer, you need a pointer and size. Cliff

YES, I tried with ":tcp::socket::send(boost::asio::buffer((void *)pf,
sizeof(pf)))" but it didn't worked.
On Fri, Oct 11, 2013 at 11:10 PM, Cliff Green
As the compiler is telling you, you can't create a buffer object with just a PacketFormat pointer, you need a pointer and size.
Cliff
______________________________**_________________ Unsubscribe & other changes: http://lists.boost.org/** mailman/listinfo.cgi/boosthttp://lists.boost.org/mailman/listinfo.cgi/boost

On 11/10/2013 19:03, Rahul Mathur wrote:
YES, I tried with ":tcp::socket::send(boost::asio::buffer((void *)pf, sizeof(pf)))" but it didn't worked. Can you be specific about what didn't work? Compile-time or runtime?
I'm pretty sure you'll find it is not asking you for the size of the pointer, but for the amount of data pointed at.

On Fri, Oct 11, 2013 at 2:03 PM, Rahul Mathur
YES, I tried with ":tcp::socket::send(boost::asio::buffer((void *)pf, sizeof(pf)))" but it didn't worked.
Did you try: std::size_t n = socket.send(boost::asio::buffer(&pf, sizeof(pf))); I don't think boost::ip::tcp::socket::send is a static function. - Trey

YES, I did tried earlier and it did worked BTW, how to know which are relative and static functions. On Sat, Oct 12, 2013 at 6:33 AM, Joseph Van Riper < fleeb.fantastique@gmail.com> wrote:
On Fri, Oct 11, 2013 at 2:03 PM, Rahul Mathur
wrote: YES, I tried with ":tcp::socket::send(boost::asio::buffer((void *)pf, sizeof(pf)))" but it didn't worked.
Did you try:
std::size_t n = socket.send(boost::asio::buffer(&pf, sizeof(pf)));
I don't think boost::ip::tcp::socket::send is a static function.
- Trey
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost

On Fri, Oct 11, 2013 at 11:58 PM, Rahul Mathur
YES, I did tried earlier and it did worked BTW, how to know which are relative and static functions.
As you look at the Asio reference, you'll see that there are 'Free functions'. Those are going to be static functions. Anything else requires an instance of a class. I think you could have also used: boost::asio::write( socket, boost::asio::buffer( &pf, sizeof(pf)), ec ); if you wanted to use free functions instead of the class's .send() function. - Trey

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!! On Sat, Oct 12, 2013 at 6:35 PM, Joseph Van Riper < fleeb.fantastique@gmail.com> wrote:
On Fri, Oct 11, 2013 at 11:58 PM, Rahul Mathur
wrote: YES, I did tried earlier and it did worked BTW, how to know which are relative and static functions.
As you look at the Asio reference, you'll see that there are 'Free functions'. Those are going to be static functions. Anything else requires an instance of a class.
I think you could have also used:
boost::asio::write( socket, boost::asio::buffer( &pf, sizeof(pf)), ec );
if you wanted to use free functions instead of the class's .send() function.
- Trey
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost

On Mon, Oct 14, 2013 at 2:32 AM, Rahul Mathur
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 ) ); - Trey

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; }

Thanks Joseph, I did try as - --- boost::asio::socket_base::keep_alive option; socket.get_option(option); if (bool is_set = option.value()) 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; --- and it worked. On Mon, Oct 14, 2013 at 4:39 PM, Joseph Van Riper < fleeb.fantastique@gmail.com> wrote:
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; }
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost

On Mon, Oct 14, 2013 at 7:56 AM, Rahul Mathur
Thanks Joseph, I did try as -
--- boost::asio::socket_base::keep_alive option; socket.get_option(option); if (bool is_set = option.value()) 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; ---
and it worked.
Notice that you are not affecting a change of the socket's option with this code. You are only reporting on the keep-alive option. And even then, you had the results reversed, reporting the option as set when it wasn't. If you want to cause the socket's option to change, you would call it's set_option function as in my original example. To clarify your code a little by not setting a variable while simultaneously testing it in an if statement, consider: boost::asio::ip::tcp::socket::keep_alive keep_alive; socket.get_option( keep_alive ); bool is_set = keep_alive.value(); if ( is_set ) { std::cout << "Socket keep-alive option is set." << std::endl; } else { std::cout << "Socket keep-alive option is not set." << std::endl; } I hope this is helpful. - Trey

true. Thanks Joe.!! On Mon, Oct 14, 2013 at 6:05 PM, Joseph Van Riper < fleeb.fantastique@gmail.com> wrote:
On Mon, Oct 14, 2013 at 7:56 AM, Rahul Mathur
wrote: Thanks Joseph, I did try as -
--- boost::asio::socket_base::keep_alive option; socket.get_option(option); if (bool is_set = option.value()) 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; ---
and it worked.
Notice that you are not affecting a change of the socket's option with this code. You are only reporting on the keep-alive option. And even then, you had the results reversed, reporting the option as set when it wasn't.
If you want to cause the socket's option to change, you would call it's set_option function as in my original example.
To clarify your code a little by not setting a variable while simultaneously testing it in an if statement, consider:
boost::asio::ip::tcp::socket::keep_alive keep_alive; socket.get_option( keep_alive ); bool is_set = keep_alive.value(); if ( is_set ) { std::cout << "Socket keep-alive option is set." << std::endl; } else { std::cout << "Socket keep-alive option is not set." << std::endl; }
I hope this is helpful.
- Trey
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
participants (4)
-
Cliff Green
-
james
-
Joseph Van Riper
-
Rahul Mathur