简体   繁体   English

Boost Asio TCP Server 处理多个客户端

[英]Boost Asio TCP Server Handling multiple clients

I am new to network programming and the usage of Boost Asio library.我是网络编程和 Boost Asio 库的新手。

I successfully implemented a task for my requirement by modifying the Boost Asio "Blocking TCP Echo Server and Client" which performs transactions of operations between my Client and Server.我通过修改在我的客户端和服务器之间执行操作事务的 Boost Asio “阻塞 TCP Echo 服务器和客户端”成功地实现了我的要求。

Now, I have a requirement where I need to connect multiple Clients with my Server.现在,我需要将多个客户端与我的服务器连接起来。

I found some relevant links suggesting the usage of async_accept at the Server side.我发现了一些相关链接,建议在服务器端使用async_accept

So, I tried running the Boost Asio example: "Async TCP Echo Server" with the "Blocking TCP Echo client" , where the server distinguishes the different clients and addresses them accordingly.因此,我尝试运行 Boost Asio 示例: “Async TCP Echo Server”和“Blocking TCP Echo client” ,其中服务器区分不同的客户端并相应地对它们进行寻址。

But, my actual requirement should be like, instead of the Server completing the entire process for one Client, it [the server] has to perform same operations for the first client then go to the second client and perform those operations and then again come back to the first client and continue in this order until all operations are complete.但是,我的实际要求应该是,不是服务器为一个客户端完成整个过程,而是 [服务器] 必须为第一个客户端执行相同的操作,然后转到第二个客户端执行这些操作,然后再次返回到第一个客户端并按此顺序继续,直到所有操作完成。

Is there any way or idea which could help me perform this flow using Boost Asio?有什么方法或想法可以帮助我使用 Boost Asio 执行此流程? Also I'm just using the "Blocking TCP Echo Client", which just has a normal connect() and not an async_connect() , now is that a problem?另外,我只是使用“阻塞 TCP Echo 客户端”,它只有一个普通的connect()而不是async_connect() ,现在这是一个问题吗?

Also, is it possible to communicate between multiple clients through the server using Boost Asio?另外,是否可以使用 Boost Asio 通过服务器在多个客户端之间进行通信?

Thanking you very much in advance!非常感谢您提前!

There are 2 models to handling multiple client concurrently on the server.有两种模型可以在服务器上同时处理多个客户端。

The one is to spawn a new thread for each client and then each thread handles each client synchronously.一种是为每个客户端生成一个新线程,然后每个线程同步处理每个客户端。 The second model is to use asynchronous APIs on a single thread all operating on a single service.第二种模型是在单个线程上使用异步 API,所有线程都在单个服务上运行。 When the accept completes, you then create a new worker thread and start the worker off the send and recv required by your protocol.当接受完成时,您然后创建一个新的工作线程并从您的协议所需的发送和接收开始工作。 You main thread goes back the accepting new connections.您的主线程返回接受新连接。

With async, you prime the pump with an async accept and the call io_service run.使用异步,您可以使用异步接受和调用 io_service 来启动泵。 When the accept completes, your callback runs.当接受完成时,您的回调将运行。 You now prime the pump again with further accepts (for more client) start async send and recv for the newly created client.您现在再次启动泵,进一步接受(对于更多客户端)为新创建的客户端启动异步发送和接收。 Since all sends and recvs are non-blocking, the only time your thread sleeps is when it has nothing to do.由于所有发送和接收都是非阻塞的,因此您的线程只有在无事可做时才会休眠。 Otherwise the io_service run method takes care of everything for you.否则 io_service run 方法会为您处理一切。

If you are blocking on sends and recvs, through, you cannot process more than one client concurrently.如果您阻塞了发送和接收,则不能同时处理多个客户端。

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

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