简体   繁体   English

Boost ASIO TCP 服务器架构

[英]Boost ASIO TCP server architecture

Im new to either boost and asio (but not to network programming) and Im currently trying to create a TCP server accepting multiple clients.我是 boost 和 asio 的新手(但不是网络编程),我目前正在尝试创建一个接受多个客户端的 TCP 服务器。 The primary functionnality of the server with clients is simple :带有客户端的服务器的主要功能很简单:

1- Read a Header with a fixed size; 1-读取一个固定大小的Header;

2- Interpret what kind of request it is, read the rest of the packet; 2-解释它是什么类型的请求,阅读数据包的其余部分;

3- Handle request with all the data; 3- 处理包含所有数据的请求;

4- Going back to step 1. 4- 回到步骤 1。

This is working with basic requests like : Checking Credentials, Enabling a FTP protocol, etc... But there is a problem : One request demands to communicate with another connected client through the server.这适用于基本请求,例如:检查凭据、启用 FTP 协议等...但是有一个问题:一个请求需要通过服务器与另一个连接的客户端进行通信。 But, if I follow the Boost Asio architecture given in this example of the Boost website但是,如果我遵循Boost 网站示例中给出的 Boost Asio 架构

Or the one in this conference或者这个会议上的那个

I don't actually have access to the other clients socket, they are not stocked, they live thanks to the behaviour of smart pointers.我实际上无法访问其他客户端套接字,它们没有库存,它们的生存归功于智能指针的行为。

I indeed have a solution for this problem, its to keep the connected sockets in list, and to share it with every connected socket, so at every time the client can access to other clients socket.对于这个问题,我确实有一个解决方案,它将连接的套接字保留在列表中,并与每个连接的套接字共享,因此每次客户端都可以访问其他客户端的套接字。 But I really think that this is not a good architecure, and that it could be better.但我真的认为这不是一个好的架构,它可以更好。

Is there any known way to resolve this problem ?有没有已知的方法来解决这个问题?

Here is a good approach:这是一个很好的方法:

  • server keeps socket list for all connected clients服务器为所有连接的客户端保留套接字列表
  • each client receives an ID (string or int) that can be easily sent across the network每个客户端都会收到一个可以轻松通过网络发送的 ID(字符串或整数)
  • client with ID "1" sends to server a message for client with ID "2" ID 为“1”的客户端向服务器发送一条消息给 ID 为“2”的客户端
  • server receives message from "1" and uses the stored socket to send to "2"服务器从“1”接收消息并使用存储的套接字发送到“2”

This way only the server keeps the sockets.这样只有服务器保留套接字。 And all clients uses IDs to identify one another.所有客户端都使用 ID 来识别彼此。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM