Connect to a socket in C++ and Boost Asio in conditional case
I have an application which acts like a client which uses Boost Asio in order to connect to a server. It works good, but i'd like to change its behavior.
At the moment, when i start it, the application immediately try to connect to the server and if this not happens (because the server is down) then the application doesn't start.
I'd like to use a conditional case in order to decide at runtime when the application have to start the connection with the server: in this way, i can use the other application features whitout having to connect to the server.
I tryed to fill in a conditional case the code which creates the io_obejct and try to connect to a socket but it doesn't work:
if (mode == A_CONDITION) {
using boost::asio::ip::tcp;
boost::asio::io_service io_service;
tcp::resolver resolver(io_service);
tcp::resolver::query query(tcp::v4(), server, boost::lexical_cast<string>(porta));
tcp::resolver::iterator iterator = resolver.resolve(query);
tcp::socket s(io_service);
s.connect(*iterator);
}
because since "s" is defined in the if statement, it is out of scope in the rest of the code so i doesn't compile:
In function
/home/user/src/main.cpp:960:26: error:'int main(int, char**)'
:'s'
was not declared in this scope /home/user/src/main.cpp:972:60: error:'s'
was not declared in this scope /home/user/src/main.cpp:1080:60: error:'s'
was not declared in this scope
This happens because i use three times
boost::asio::write(s, boost::asio::buffer(heading_data, heading_length));
outer of the if conditional block.
I can't include the writing on the socket in the conditional case block so i think i have to use a global definition of 's' but i don't know how to do this or if there is another better solution.
I hope you can help me :)
EDIT for pure cuteness: If i do thi开发者_StackOverflow社区s:
tcp::socket *s = 0;
boost::asio::io_service io_service;
tcp::resolver resolver(io_service);
tcp::resolver::query query(tcp::v4(), server, boost::lexical_cast<string>(porta));
tcp::resolver::iterator iterator = resolver.resolve(query);
if (mode = FOLLOW_MODE){
s = new tcp::socket(io_service);
s->connect(*iterator);
}
It gives me these errors during the compilation:
> In file included from
> /usr/include/boost/asio/write.hpp:526:0,
> from /usr/include/boost/asio/buffered_write_stream.hpp:32,
> from /usr/include/boost/asio/buffered_stream.hpp:26,
> from /usr/include/boost/asio.hpp:34,
> from /home/user/src/main.cpp:9:
> /usr/include/boost/asio/impl/write.ipp:
> In function 'size_t
> boost::asio::write(SyncWriteStream&,
> const ConstBufferSequence&,
> CompletionCondition,
> boost::system::error_code&) [with
> SyncWriteStream =
> boost::asio::basic_stream_socket<boost::asio::ip::tcp>*, ConstBufferSequence =
> boost::asio::const_buffers_1,
> CompletionCondition =
> boost::asio::detail::transfer_all_t,
> size_t = unsigned int]':
> /usr/include/boost/asio/impl/write.ipp:57:71:
> instantiated from 'size_t
> boost::asio::write(SyncWriteStream&,
> const ConstBufferSequence&) [with
> SyncWriteStream =
> boost::asio::basic_stream_socket<boost::asio::ip::tcp>*, ConstBufferSequence =
> boost::asio::const_buffers_1, size_t =
> unsigned int]'
> /home/user/src/main.cpp:980:78:
> instantiated from here
> /usr/include/boost/asio/impl/write.ipp:44:57:
> error: request for member 'write_some'
> in 's', which is of non-class type
> 'boost::asio::basic_stream_socket<boost::asio::ip::tcp>*' make[2]: ***
> [src/CMakeFiles/main.dir/main.cpp.o]
> Error 1 make[1]: ***
> [src/CMakeFiles/main.dir/all] Error 2
> make: *** [all] Error 2
Look at handle_commit()
in chat_client.cpp
. If there is an error, instead of closing the connection, attempt to reconnect.
tcp::socket *s = 0;
if (mode == A_CONDITION) {
s = new tcp::socket(io_service);
s->connect(*iterator);
}
ok? Don't forget to delete s
later.
精彩评论