C++:一个客户端与多个服务器通信
C++: One client communicating with multiple server
我想知道,是否可以让一个客户端同时与多个服务器通信。据我所知,像火狐这样的常见浏览器正是这样做的。
我现在遇到的问题是,客户端必须侦听并等待来自服务器的数据,而不是请求它本身。它必须一次侦听多个服务器。这可能吗?如果客户端正在侦听服务器 1 并且服务器 2 发送内容,会发生什么情况?包裹是丢失还是会重新发送,直到客户端传达成功接收?使用的协议是TCP。
编辑:平台是Windows。感谢您指出这一点 阿伦穆.
这与使用 select/poll/epoll 或使用线程池或使用每个连接的进程或您所知道的任何模型的常规套接字编程没有什么不同。
我可以向您展示有关如何使用 epoll 执行此操作的粗略伪代码。注意:我的函数都不存在C++,它只是为了解释目的。我也假设你在Linux上,因为你没有提到这个平台。
socket sd = connect("server.com", 8080);
sd.set_nonblocking(1);
epoll_event event;
event.data.fd = sd
epoll_ctl(ADD, event);
...
...
while (True) {
auto n = epoll_wait(events, 1);
for (int i : 1...n) {
if (events[i].data.fd == sd) // The socket added in epoll_ctl
{
std::thread(&Session::read_handler, rd_hndler_, sd); // Call the read in another thread or same thread
}
}
}
我希望你明白了要点。从本质上讲,将服务器视为客户端,将客户端视为服务器,您的问题就解决了(有点)。查看下面的链接以了解有关epoll
的更多信息https://banu.com/blog/2/how-to-use-epoll-a-complete-example-in-c/
要查看使用 epoll 的全功能服务器设计,请查看:
https://github.com/arun11299/cpp-reactor-server/blob/master/epoll/reactor.cc
相关文章:
- 提升::Asio 异步聊天客户端停止与服务器通信
- 如何让客户端通过modbus-tcp协议与多个服务器通信?
- 与语言服务器通信(Languager服务器协议)
- 使用UNIX套接字C++客户端与Node.js服务器通信的简单示例
- 证书验证在客户端服务器通信中使用boost :: asio和openssl失败
- C++ Linux 代理服务器通信
- C++:一个客户端与多个服务器通信
- 未经身份验证的SMTP服务器到服务器通信
- C++ 客户端无法与 Java 服务器通信
- 使用 C/C++、SSL 协议的客户端和服务器通信不起作用
- 如果传递长类型,32位客户端如何与64位服务器通信
- Raspberry Pi 和 Android 之间的客户端-服务器通信
- 服务中的 DDE 客户端与用户会话中的服务器通信
- 为客户端-服务器通信构建TCP/IP服务器的最佳方式
- QTcpSocket双向客户端-服务器通信
- 如何在一个套接字上实现并行请求和响应的非阻塞客户端-服务器通信模型,而不存在数据竞争
- WinSock2 客户端/服务器通信:发送和接收字符串
- 客户端-服务器通信:并非所有元素的发送和接收都使用套接字
- 我无法让多个小程序与我的 Cpp 服务器通信
- java客户端通过TCP连接与c++服务器通信