Connect to a socket in C ++ and Boost Asio in a conditional state

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(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 'int main(int, char**)'
:

/home/user/src/main.cpp:960:26: error: '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 this:

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(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*, 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*, 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*' make[2]: ***
> [src/CMakeFiles/main.dir/main.cpp.o]
> Error 1 make[1]: ***
> [src/CMakeFiles/main.dir/all] Error 2
> make: *** [all] Error 2
Hello, buddy!稿源:Hello, buddy! (源链) | 关于 | 阅读提示

本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 综合编程 » Connect to a socket in C ++ and Boost Asio in a conditional state

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录