提升asio - 同步写/读 - 怎么办?

时间:2023-01-28 20:58:47

First, I want to say that I'm new with Boost asio, and I see a lot of examples but it remains things I don't understand.

首先,我想说我是Boost asio的新手,我看到很多例子,但它仍然是我不明白的事情。

I want to create a server, that will accept two clients (it will use two socket). The first client will send messages to the server and the server will send this message to the other client (yes, it is useless to use a server, but it's not the point here, I want to understand how all this work). This will happen until one of the client close.

我想创建一个服务器,它将接受两个客户端(它将使用两个套接字)。第一个客户端将消息发送到服务器,服务器将此消息发送给另一个客户端(是的,使用服务器是没用的,但这不是重点,我想了解这一切是如何工作的)。这将发生在客户关闭之前。

So, I created a server, the server wait for the clients, and then, it must wait for the first client to send some message. And this is my question: what must I do after?

所以,我创建了一个服务器,服务器等待客户端,然后,它必须等待第一个客户端发送一些消息。这是我的问题:我必须做什么?

I thought I need to read the first socket, and then write on the second, and so and so, but how I know if the first client writed on the socket? Same, how I know if the second client read the second socket?

我以为我需要读取第一个套接字,然后写第二个,所以,所以,但我怎么知道第一个客户端是否在套接字上写入?同样,我怎么知道第二个客户端是否读取了第二个套接字?

I don't need code, I just want to know the good way to do that.

我不需要代码,我只是想知道这样做的好方法。

Thanks a lot for reading!

非常感谢阅读!

1 个解决方案

#1


0  

When you perform async_read you specifify a callback which is going to be called whenever any data is read to the buffer ( you should provide the buffer also, check the async_read's documentation ). Respectively you should provide callback for the async_write to know when your data is already sent. So, from the server perspective, for the client which 'writes' you should do async_read, and for the second client which 'reads' you should do async write. With the offered dataflow client1->server->client2 it is hard to recognize which client the server should read from and which one is write to. It's up to you. You can choose the first connected client as writer and the second as reader, for example.

当你执行async_read时,你指定一个回调,只要有任何数据被读入缓冲区就会被调用(你也应该提供缓冲区,检查async_read的文档)。您应该为async_write提供回调,以了解您的数据何时已发送。因此,从服务器的角度来看,对于“写入”的客户端,您应该执行async_read,对于“读取”的第二个客户端,您应该执行异步写入。使用提供的数据流client1-> server-> client2,很难识别服务器应该读取哪个客户端以及写入哪个客户端。由你决定。例如,您可以选择第一个连接的客户端作为编写器,第二个作为读取器。

You might want to start with asio iostreams. It's a high-level iostream-like abstraction above asynchronous sockets.

您可能想要从asio iostreams开始。它是异步套接字之上的高级iostream式抽象。

P.S.: also, don't forget to run io_service.run() loop somewhere. Because all the asio callbacks are executed within that loop.

P.S。:同样,不要忘记在某处运行io_service.run()循环。因为所有asio回调都在该循环中执行。

#1


0  

When you perform async_read you specifify a callback which is going to be called whenever any data is read to the buffer ( you should provide the buffer also, check the async_read's documentation ). Respectively you should provide callback for the async_write to know when your data is already sent. So, from the server perspective, for the client which 'writes' you should do async_read, and for the second client which 'reads' you should do async write. With the offered dataflow client1->server->client2 it is hard to recognize which client the server should read from and which one is write to. It's up to you. You can choose the first connected client as writer and the second as reader, for example.

当你执行async_read时,你指定一个回调,只要有任何数据被读入缓冲区就会被调用(你也应该提供缓冲区,检查async_read的文档)。您应该为async_write提供回调,以了解您的数据何时已发送。因此,从服务器的角度来看,对于“写入”的客户端,您应该执行async_read,对于“读取”的第二个客户端,您应该执行异步写入。使用提供的数据流client1-> server-> client2,很难识别服务器应该读取哪个客户端以及写入哪个客户端。由你决定。例如,您可以选择第一个连接的客户端作为编写器,第二个作为读取器。

You might want to start with asio iostreams. It's a high-level iostream-like abstraction above asynchronous sockets.

您可能想要从asio iostreams开始。它是异步套接字之上的高级iostream式抽象。

P.S.: also, don't forget to run io_service.run() loop somewhere. Because all the asio callbacks are executed within that loop.

P.S。:同样,不要忘记在某处运行io_service.run()循环。因为所有asio回调都在该循环中执行。