- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在研究套接字的 Asio 文档,但找不到任何关于如何处理以下情况的有用信息:
我假设在对等网络中有很多服务器(最多 1000 个)。服务器必须定期相互通信,所以我不想在每次需要时打开一个新的客户端连接来向另一台服务器发送消息(巨大的开销)。
同时,创建 n 个线程,每个线程对应一个客户端 -> 服务器连接也不太可行。
我将实现不同的通信方案(全对全、星形和树形),因此 1、log(n) 和 n 个服务器必须实例化这 n 个套接字客户端以创建到其他服务器的连接。
有没有我可以简单做的好方法(伪代码)。
pool = ConnectionPool.create(vector<IP>);
pool.sendMessage(ip, message);
我知道在服务器端我可以使用异步连接。但是,我真的不知道如何从 C++/Asio 的“客户端”(发送方)角度处理它。
Tl:DR;
Which APIs and classes am I supposed to use when I want to "send" messages to N servers without having to open N connections every time I do that and neither using N threads".
最佳答案
Yes, each process will need a server side (to receive messages from any of the n participants) and one client side (to send messages to any of the n participants). However, as far as I could find in Asio, the only way to send messages to k of the n participants is by creating k threads with k connections
那你一定是没看对地方,或者根本没看多远。
异步 IO 的核心原则是在单个线程上多路复用 IO(所有 kqueue/epoll/select/IO 完成端口等抽象都针对该目标)。
这是一个绝对延迟编码的演示,显示:
在心跳间隔内,我们向所有对等点发送心跳消息
for (auto& peer : peers)
async_write(peer, buffer(message), [ep=peer.remote_endpoint(ec)](error_code ec, size_t xfr) {
std::cout << "(sent " << xfr << " bytes to " << ep << "(" << ec.message() << ")" << std::endl;
});
#include <boost/asio.hpp>
#include <list>
#include <iostream>
using std::tuple;
using namespace std::literals;
template <typename T>
static auto reference_eq(T const& obj) {
return [p=&obj](auto& ref) { return &ref == p; };
}
int main() {
using namespace boost::asio; // don't be this lazy please
using boost::system::error_code;
using ip::tcp;
io_context ioc;
tcp::acceptor listener(ioc, {{}, 6868});
listener.set_option(tcp::acceptor::reuse_address(true));
listener.listen();
using Loop = std::function<void()>;
std::list<tcp::socket> clients, peers;
// accept unbounded clients
Loop accept_loop = [&] {
listener.async_accept([&](error_code const& ec, tcp::socket s) {
if (!ec) {
std::cout << "New session " << s.remote_endpoint() << std::endl;
clients.push_back(std::move(s));
accept_loop();
}
});
};
tcp::resolver resoler(ioc);
for (auto [host,service] : {
tuple{"www.example.com", "http"},
{"localhost", "6868"},
{"::1", "6868"},
// ...
})
{
auto& p = peers.emplace_back(ioc);
async_connect(p, resoler.resolve(host,service), [&,spec=(host+":"s+service)](error_code ec, auto...) {
std::cout << "For " << spec << " (" << ec.message() << ")";
if (!ec)
std::cout << " " << p.remote_endpoint();
else
peers.remove_if(reference_eq(p));
std::cout << std::endl;
});
}
std::string const& message = "heartbeat\n";
high_resolution_timer timer(ioc);
Loop heartbeat = [&]() mutable {
timer.expires_from_now(2s);
timer.async_wait([&](error_code ec) {
std::cout << "heartbeat " << ec.message() << std::endl;
if (ec)
return;
for (auto& peer : peers)
async_write(peer, buffer(message), [ep=peer.remote_endpoint(ec)](error_code ec, size_t xfr) {
std::cout << "(sent " << xfr << " bytes to " << ep << "(" << ec.message() << ")" << std::endl;
});
heartbeat();
});
};
signal_set sigs(ioc, SIGINT, SIGTERM);
sigs.async_wait([&](error_code ec, int sig) {
if (!ec) {
std::cout << "signal: " << strsignal(sig) << std::endl;
listener.cancel();
timer.cancel();
} });
accept_loop();
heartbeat();
ioc.run_for(10s); // max time for Coliru, or just `run()`
}
打印(在我的系统上):
New session 127.0.0.1:46730
For localhost:6868 (Success) 127.0.0.1:6868
For ::1:6868 (Connection refused)
For www.example.com:http (Success) 93.184.216.34:80
heartbeat Success
(sent 10 bytes to 93.184.216.34:80(Success)
(sent 10 bytes to 127.0.0.1:6868(Success)
heartbeat Success
(sent 10 bytes to 93.184.216.34:80(Success)
(sent 10 bytes to 127.0.0.1:6868(Success)
heartbeat Success
(sent 10 bytes to 93.184.216.34:80(Success)
(sent 10 bytes to 127.0.0.1:6868(Success)
^Csignal: Interrupt
heartbeat Operation canceled
Note how the one client ("New session") is our own peer connection on localhost:6868 :)
当然,在现实生活中你会有一个类来表示客户端 session ,可能有等待发送消息的队列,并且可以选择在多个线程上运行(使用 strand
来同步对共享的访问对象)。
如果您真的希望避免显式收集客户端,请参阅这个非常相似的演示:How to pass a boost asio tcp socket to a thread for sending heartbeat to client or server哪个
¹ 由于网络访问受限,它不适用于 coliru。不使用解析器的仅环回版本有效:Live On Coliru
关于c++ - Asio 点对点网络编程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62343684/
我正在尝试实现这个协议(protocol):http://en.wikipedia.org/wiki/Chord_(peer-to-peer ) 我从中了解到的是,加入“圆圈”的每个节点都放置在圆圈内
我对 java 中的 cometd 很陌生。 我对 java 中的 cometd 更感兴趣,但是当我用 google 搜索它时,我几乎找不到一个链接这是 cometd 链接,文档中不清楚。 有人可以发
什么是编写 XNA 点对点游戏的最佳方式而不必使用要求游戏的两个玩家都具有 XBOX Gold 成员(member)资格的 Windows Live 东西 我还需要一些客户端/服务器功能,但这还不是很
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
假设我有 2 部手机,彼此相距 50 米,我想从手机 A 向手机 B 发送一个非常小的数据包,而不使用与蜂窝塔的任何通信。 为了简单起见,我想构建一个应用程序,为同一半径(同一区域)的 2 部手机实现
我听说过点对点内存传输并阅读了一些关于它的内容,但无法真正理解与标准 PCI-E 总线传输相比它的速度有多快。 我有一个使用多个 GPU 的 CUDA 应用程序,我可能对 P2P 传输感兴趣。我的问题
我从Android website中发现了这一点他们告诉我当前的 API使开发人员能够为 Wifi 点对点进行一些编程仅适用于 Android 4.0(API 级别 14)。 这是真的吗?我的意思是我
我正在为网页编写一个简单的 javascript 游戏。我将使用 tidesdk 将其转换为桌面。我想让不同机器上的玩家无需通过服务器进行通信。 一般来说这可能吗?这是套接字??您是否有使用 Java
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 7 年前。 Improve this q
我想在我的 iPhone 应用程序中加入 Paypal 作为点对点选项来回馈 friend 。然而,当我在网上搜索 Paypal iOS 时,它说我现在应该使用 Braintree。 它非常易于使用并
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 4 年前。 Improve this qu
我正在寻找一种使用 p2p 将客户端(网络浏览器)连接到服务器(没有外部 IP)的方法。 作为客户端语言,我想使用 javascript。 我正在阅读有关 WebRTC 点对点的信息,但我不知道它是否
我在 .NET 3.5 中使用 WCF 来实现对等网络应用程序。我使用 PNRP 解析对等节点。 IGlobalStoreServiceContract 是我的契约(Contract),如下所示, [
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
是否有任何已知的方法可以在不使用专用中央服务器的情况下找到对等点? 即:如果我有对等方断开并重新连接到互联网但每次都获得一个新的 IP 地址,并且我想连接到他们而不设置专用服务器进行注册。 我正在考虑
我正在尝试将 Paypal 的自适应支付 API 集成到我的应用程序中,将只使用点对点交易,但我无法做任何与点对点支付相关的事情。我尝试使用这个工具 from Paypal 但我仍然没有取得任何成功。
我正在编写用于交换文本消息的点对点(它不应该有服务器 - 这是一项任务)程序。这是一个非常小的聊天。只是消息,没有别的。这是我第一次练习 Boost::Asio,因此我有一些问题。 正如我所说,我的聊
很难说出这里问的是什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或言辞激烈,无法以目前的形式合理回答。如需帮助澄清此问题以便可以重新打开,visit the help center . 9年前关闭
我想使用 Python 和 Ubuntu 在两台计算机之间双向传输音频,最好使用 H.323 我看过 pjsip,但只能看到一种连接到 SIP 服务器的方式,而不是简单的点对点系统。 谁能指出我正确的
我有两个使用 vert.x EventBus 进行通信的 Java 类。 我有一个 Productor.java 类: package TP1; import io.vertx.core.Abstra
我是一名优秀的程序员,十分优秀!