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 <boost/asio.hpp> namespace opp = boost::asio::ip; struct PacketFormat { unsigned short length; //Length int sequenceNumber; }; int main(int argc, char* argv[]) { std::string port = "8000"; std::string hostname = "X.X.X.X"; boost::asio::io_service io_service; opp::tcp::resolver resolver(io_service); opp::tcp::resolver::query query(hostname, port); opp::tcp::resolver::iterator iter = resolver.resolve(query); opp::tcp::resolver::iterator end; // End marker. // Looking for a successful endpoint connection opp::tcp::socket socket(io_service); boost::system::error_code error = boost::asio::error::host_not_found; while (error && iter != end) { socket.close(); socket.connect(*iter++ , error); } if (error) throw boost::system::system_error(error); std::cout << "Connected ..Thanks!! \n" << std::endl; PacketFormat pf; // where PacketFormat is struct defined pf.length = 236; pf.sequenceNumber = 2; std::cout << "Sending Logon Message.. . ..!! \n" << std::endl; std::size_t n = opp::tcp::socket::send(iter, boost::asio::buffer(&pf)); if (n < 0) std::cerr << "ERROR writing to Server .. . ..\n" << std::endl; std::cout << "Logon Message Sent.. . ..Thanks!!\n" << std::endl; if (n < 0) std::cerr << "ERROR writing to Server .. . ..\n" << std::endl; std::cout << "Logon Message Sent.. . ..Thanks!!\n" << std::endl; return 0; } __ The error message obtained is - -- # g++ testclient.cpp -o testclient -lboost_system -lboost_thread testclient.cpp: In function âint main(int, char**)â: testclient.cpp:42: error: no matching function for call to âbuffer(PacketFormat*)â /usr/local/include/boost/asio/buffer.hpp:704: note: candidates are: boost::asio::mutable_buffers_1 boost::asio::buffer(const boost::asio::mutable_buffer&) /usr/local/include/boost/asio/buffer.hpp:717: note: boost::asio::mutable_buffers_1 boost::asio::buffer(const boost::asio::mutable_buffer&, size_t) /usr/local/include/boost/asio/buffer.hpp:733: note: boost::asio::const_buffers_1 boost::asio::buffer(const boost::asio::const_buffer&) /usr/local/include/boost/asio/buffer.hpp:746: note: boost::asio::const_buffers_1 boost::asio::buffer(const boost::asio::const_buffer&, size_t) /usr/local/include/boost/asio/buffer.hpp:762: note: boost::asio::mutable_buffers_1 boost::asio::buffer(void*, size_t) /usr/local/include/boost/asio/buffer.hpp:772: note: boost::asio::const_buffers_1 boost::asio::buffer(const void*, size_t) __ Please help for L#42 "tcp::socket::send()" proper call. I believe above code fine apart from L#42 error. Thanks!!
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 <cliffg@codewrangler.net>wrote:
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/boost<http://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 <srivmuk@gmail.com> 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
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 <srivmuk@gmail.com> 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 <srivmuk@gmail.com> 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
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 <srivmuk@gmail.com> 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 <srivmuk@gmail.com> 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 ) ); - 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 <srivmuk@gmail.com> 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 <srivmuk@gmail.com> 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 <srivmuk@gmail.com> 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
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 <srivmuk@gmail.com> 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