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

综合编程 Hello, buddy! (源链)

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

您可能感兴趣的

Fault Tolerance doesn’t come out of the box ... Alchemy 101: Part 3 - Fault Tolerance doesn’t come out of the box One of the biggest selling points of Elixir is the means it gives you to write fa...
Dealing with the running conditions of boost threa... I have 6 threads running in my application continuously. The scenario is: One thread continuously gets the messages and inserts into a message qu...
HP-Socket v5.0.1:支持 IPv6 及多 SSL 证书 HP-Socket 是一套通用的高性能 TCP/UDP/HTTP 通信框架,包含服务端组件、客户端组件和 Agent 组件,广泛适用于各种不同应用场景的 TCP/UDP/HTTP 通信系统,提供 C/C++、C#、Delphi、E(易语言)、Java、Python 等编程语言接口。HP-S...
Linux网络栈解剖(Anatomy of the Linux networking stack)... 最近和屈总伯温一起看linux协议栈,正好回忆了下14-15赛季做FTCP的时候看的一些linux network kernel的代码,找到了这篇文章,写的很好。原文为 Anatomy of the Linux networking stack, From sockets to device...
修改了一个爬虫htcap 修改htcap的数据库为mysql 好懒..拖延症.....本来早两个月以前就应该完成了的东西.在基友的催促下匆忙的修改了一下.修改了一个爬虫htcap,这个爬虫强大之处就是基于PhantomJS 实现的.数据库本来这个是基于sqlite3的.本屌感觉这个爬虫其实功能还是不错的.于是就修改了数据...
Hello, buddy!责编内容来自:Hello, buddy! (源链) | 更多关于

阅读提示:酷辣虫无法对本内容的真实性提供任何保证,请自行验证并承担相关的风险与后果!
本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » Connect to a socket in C ++ and Boost Asio in a conditional state



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

使用声明 | 英豪名录