[asio] sending multiple messages in the same soocket
Hi! I'm trying to implement an asynchronous client to communicate with a server using asio. To do this, I'm following the example of the http client ( http://tenermerx.com/Asio/asio-1.3.1/src/examples/http/client/async_client.c... http://tenermerx.com/Asio/asio-1.3.1/src/examples/http/client/async_client.c... ), but I'm trying to make it so that it can be used for a more general purpose. So in my implementation I have the same methods that appear in the example, but with only one method for reading the response. My main function calls the constructor of the client which is responsible for initializing variables and calling the chain of asynchronous methods. After this, the main function calls a method send(message), that puts the message I want to send in the variable request and runs the io_service. The problem is that I want to be able to call this send function several times, but it only works the first time I call it. For example: Client c(host, port); //remember that the constructor calls the asynchronous methods c.send("msg to send"); c.send("msg to send"); The message is actually sent the first time, but the rest of the times nothing happens. What am I missing? Can you help me? If you need more information to be able to help, please ask me. Henrique Aparício -- View this message in context: http://www.nabble.com/-asio--sending-multiple-messages-in-the-same-soocket-t... Sent from the Boost - Users mailing list archive at Nabble.com.
The problem is that I want to be able to call this send function several times, but it only works the first time I call it. For example:
Client c(host, port); //remember that the constructor calls the asynchronous methods c.send("msg to send"); c.send("msg to send");
The message is actually sent the first time, but the rest of the times nothing happens. What am I missing? Can you help me?
Obviously, it's hard to guess how your Client::send is implemented, but if it doesn't work as expected, some bug is there.
This is my function Client::send(): void Client::send(const string &message) { ostream request_stream(&request); request_stream << message; io_service.run(); } Igor R wrote:
The problem is that I want to be able to call this send function several times, but it only works the first time I call it. For example:
Client c(host, port); //remember that the constructor calls the asynchronous methods c.send("msg to send"); c.send("msg to send");
The message is actually sent the first time, but the rest of the times nothing happens. What am I missing? Can you help me?
Obviously, it's hard to guess how your Client::send is implemented, but if it doesn't work as expected, some bug is there.
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
-- View this message in context: http://www.nabble.com/-asio--sending-multiple-messages-in-the-same-soocket-t... Sent from the Boost - Users mailing list archive at Nabble.com.
This is my function Client::send():
void Client::send(const string &message) {
ostream request_stream(&request); request_stream << message;
io_service.run(); }
What are you doing with request_stream? I don't see in your send() function
any call to async_write or so. Besides, running io_service here doesn't seem to make sense... Please, refer to the tutorial to understand the basic usage patterns of asio: http://www.boost.org/doc/libs/1_37_0/doc/html/boost_asio/tutorial.html
When I do request_stream << message; I'm putting the content of message into the variable request. The variable request is a private variable that belongs to the Client class. As I said before, the asynchronous functions are called first (the constructor calls async_resolve, which calls async_connect, which calls async_write, which calls async_read). Only then is the send function called from the main function. Anyway, I'm trying to do this for UDP as well, and only now I manged to get it to work. I had to do io_service.reset() before I did io_service.run(). I guess it should be something like this in the TCP solution. I'm sorry if I'm not very good at explaining myself, but I'm new using the boost library as well as new to this forum. Thanks for your attention, Henrique Aparício Igor R wrote:
This is my function Client::send():
void Client::send(const string &message) {
ostream request_stream(&request); request_stream << message;
io_service.run(); }
What are you doing with request_stream? I don't see in your send() function
any call to async_write or so. Besides, running io_service here doesn't seem to make sense... Please, refer to the tutorial to understand the basic usage patterns of asio: http://www.boost.org/doc/libs/1_37_0/doc/html/boost_asio/tutorial.html
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
-- View this message in context: http://www.nabble.com/-asio--sending-multiple-messages-in-the-same-soocket-t... Sent from the Boost - Users mailing list archive at Nabble.com.
When I do request_stream << message; I'm putting the content of message into the variable request. The variable request is a private variable that belongs to the Client class. As I said before, the asynchronous functions are called first (the constructor calls async_resolve, which calls async_connect, which calls async_write, which calls async_read). Only then is the send function called from the main function.
Ok, now it's more clear. Lets see what happens, step by step: 1) you create Client object: async_resolve is queued to the io_service object, which is not running yet 2) you call Client::Send - io_service::run is called (note: the caller of Client::Send() will now wait until (8) ) 3) io_service starts dispatching the pending async_resolve request 4) handler of async_resolve is called, which in turn queues async_connect 5) handler of async_connect is called - async_write is queued; at this point your buffer is consumed and sent to the peer 6) handler of async_write is called - async_read is queued 7) etc... until at some stage read/write chain ends, i.e. all handlers are done and no more i/o request is queued 8) only *now*, when there's no more work, io_service::run exits, and so does your Send() function - the caller may now continue with his stuff. Actually, you got synchronous i/o, but I guess this wasn't your intent, was it? If it was, you could just use simple synchronous functions: resolve(); connect(); write(); read() - much simpler and with the same effect. What you could do to take an advantage of the async.i/o is to run the io_service in some other thread, like in this example: http://www.boost.org/doc/libs/1_37_0/doc/html/boost_asio/example/chat/chat_c...
participants (2)
-
Henrique Aparício
-
Igor R