连接到 C++ 中的套接字以及有条件情况下的 Boost Asio

发布于 2024-11-13 23:09:04 字数 3248 浏览 1 评论 0 原文

我有一个应用程序,它的作用类似于客户端,它使用 Boost Asio 来连接到服务器。 它工作得很好,但我想改变它的行为。

目前,当我启动它时,应用程序立即尝试连接到服务器,如果没有发生这种情况(因为服务器已关闭),则应用程序不会启动。

我想使用条件情况,以便在运行时决定应用程序何时必须启动与服务器的连接:这样,我可以使用其他应用程序功能,而无需连接到服务器。

我尝试填写创建 io_obejct 的条件代码并尝试连接到套接字,但它不起作用:

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

因为由于“s”是在 if 语句中定义的,所以它超出了其余部分的范围代码所以我不编译:

在函数'int main(int, char**)'中:
/home/user/src/main.cpp:960:26: 错误:'s' 未在此范围内声明
/home/user/src/main.cpp:972:60: 错误:'s' 未在此范围内声明
/home/user/src/main.cpp:1080:60: 错误:'s' 未在此范围内声明

使用了三次

boost::asio::write(s, boost::asio::buffer(heading_data, heading_length));

这种情况是因为我在 if 条件块外部

。我无法将套接字上的写入包含在条件 case 块中,因此我认为我必须使用 '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);  
 }

它会在编译过程中给我这些错误:

> 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

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 '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<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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

赠意 2024-11-20 23:09:04

查看 handle_commit() “nofollow”>chat_client.cpp。如果出现错误,请尝试重新连接,而不是关闭连接。

Look at handle_commit() in chat_client.cpp. If there is an error, instead of closing the connection, attempt to reconnect.

柳若烟 2024-11-20 23:09:04
tcp::socket *s = 0;

if (mode == A_CONDITION) {
    s = new tcp::socket(io_service);
    s->connect(*iterator);   
}

好的?不要忘记稍后删除s

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文