[ASIO] Wrong usage of streambuf ?
Hello I m writing a simple client/server program. On a request the server answers "ok\r\n". It works well. I have a problem with the client. Using async functions, I don't manage to handle the response from the server. Once the server sent the response, I want to read the data, close the socket, and open a new connection to the server. When I don't read the data sent by the server, I manage to close the socket and repeat the operation (close the socket, and reconnect to the server). When I read the data, the program seems to block. /* Callback to the async_write function. On any request, the server sends "ok\r_n" */ void Client::handle_write(boost::asio::ip::tcp::socket* socket, const boost::system::error_code& err) { if(!err) { cout << "Read..." << endl ; // response_ is a boost::asio::streambuf variable boost::asio::async_read_until(*socket, response_, "\r\n", boost::bind(&Client::handle_read, this, socket, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred)) ; } else { cout << "Error when writing to host: " << err.message() << "\n" ; } } void Client::handle_read(boost::asio::ip::tcp::socket* socket, const boost::system::error_code& err, size_t bytes_transferred) { cout << bytes_transferred << " bytes received. " << endl ; /* Dont count the 2 bytes long "\r\n" string */ int message_size = bytes_transferred - 2 ; char * response_char = new char[message_size + 1] ; response_char[message_size] = '\0' ; response_stream.read(response_char, message_size) ; string result(response_char) ; delete response_char ; response_.consume(2) ; socket->close() ; delete socket ; connect() ; return ; } If I skip the block where I read the response_ into a string, the program works flawlessly. If I try to read the response from the streambuf variable, the program is blocked at the next call to async_read_until() */ So I guess that I m doing something wrong with the streambuf variable named "response_", but I dont understand what. Can someone explain me what is wrong ? Thanks in advance.
On Tue, Dec 8, 2009 at 8:03 AM, Axel
When I don't read the data sent by the server, I manage to close the socket and repeat the operation (close the socket, and reconnect to the server). When I read the data, the program seems to block.
I'm new to this too but I'll suggest some things. Is response_ an iostream? If so it might be that the read() operation is trying to read from the socket associated with the iostream and not the socket you've connected to the server. -- Cause united breaks guitars http://www.youtube.com/watch?v=5YGc4zOqozo
On 09/12/2009 19:38, Jay Sprenkle wrote:
On Tue, Dec 8, 2009 at 8:03 AM, Axel
mailto:axel.boost@laposte.net> wrote: When I don't read the data sent by the server, I manage to close the socket and repeat the operation (close the socket, and reconnect to the server). When I read the data, the program seems to block.
I'm new to this too but I'll suggest some things.
Is response_ an iostream? If so it might be that the read() operation is trying to read from the socket associated with the iostream and not the socket you've connected to the server.
response_ is a boost::asio::streambuf variable. I dont really understand your answer, but there is only one socket_ variable. I create a socket, connect to the server, send some bytes, read some bytes, close the socket and repeat the whole process. It blocks at the second async_read_until() , though I closed the socket and consumed the streambuf variable. Examples dont help me, no one try to repeat such a process. The problem is in the way I handle the streambuf. If I don't read the result from the streambuf, the behaviour is OK, when I try to read the streambuf, it fails.
On 10/12/2009 12:17, Igor R wrote:
Because just before, the 2 bytes of "ok" are consumed by response_stream.read(response_char, message_size) ; I assume that this read() call consume them, no ? No.
I tried to consume the amount of bytes transferred (4 in this case), without success. What makes me think that my problem is stream-related is that when I don't try to read the answer from the server, I successful close the socket , reconnect, and repeat the process without problem.
I am not really sure what response_stream is, but I see what you are trying
to do. Consider doing the following instead:
void Client::handle_read(boost::asio::ip::tcp::socket* socket, const
boost::system::error_code& err, size_t bytes_transferred)
{
...
//Note: data, will contain the "ok\r\n".. so just do some string manip to
get rid of it
const char* data=boost::asio::buffer_cast
On 10/12/2009 12:17, Igor R wrote:
Because just before, the 2 bytes of "ok" are consumed by response_stream.read(response_char, message_size) ; I assume that this read() call consume them, no ? No.
I tried to consume the amount of bytes transferred (4 in this case), without success. What makes me think that my problem is stream-related is that when I don't try to read the answer from the server, I successful close the socket , reconnect, and repeat the process without problem.
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
-- View this message in context: http://old.nabble.com/-ASIO--Wrong-usage-of-streambuf---tp26695276p26731226.... Sent from the Boost - Users mailing list archive at Nabble.com.
Axel-41 wrote:
Hello
I m writing a simple client/server program. On a request the server answers "ok\r\n". It works well. I have a problem with the client. Using async functions, I don't manage to handle the response from the server. Once the server sent the response, I want to read the data, close the socket, and open a new connection to the server.
When I don't read the data sent by the server, I manage to close the socket and repeat the operation (close the socket, and reconnect to the server). When I read the data, the program seems to block.
/* Callback to the async_write function. On any request, the server sends "ok\r_n" */ void Client::handle_write(boost::asio::ip::tcp::socket* socket, const boost::system::error_code& err) { if(!err) { cout << "Read..." << endl ; // response_ is a boost::asio::streambuf variable boost::asio::async_read_until(*socket, response_, "\r\n", boost::bind(&Client::handle_read, this, socket, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred)) ; } else { cout << "Error when writing to host: " << err.message() << "\n" ; } }
void Client::handle_read(boost::asio::ip::tcp::socket* socket, const boost::system::error_code& err, size_t bytes_transferred) { cout << bytes_transferred << " bytes received. " << endl ;
/* Dont count the 2 bytes long "\r\n" string */ int message_size = bytes_transferred - 2 ;
char * response_char = new char[message_size + 1] ; response_char[message_size] = '\0' ; response_stream.read(response_char, message_size) ; string result(response_char) ; delete response_char ; response_.consume(2) ;
socket->close() ; delete socket ; connect() ; return ; }
If I skip the block where I read the response_ into a string, the program works flawlessly. If I try to read the response from the streambuf variable, the program is blocked at the next call to async_read_until() */ So I guess that I m doing something wrong with the streambuf variable named "response_", but I dont understand what. Can someone explain me what is wrong ?
Thanks in advance.
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
I am not really sure what response_stream is, but I see what you are trying
to do. Consider doing the following instead:
void Client::handle_read(boost::asio::ip::tcp::socket* socket, const
boost::system::error_code& err, size_t bytes_transferred)
{
...
//Note: data, will contain the "ok\r\n".. so just do some string manip to
get rid of it
const char* data=boost::asio::buffer_cast
participants (5)
-
Axel
-
Igor R
-
Imran Fanaswala
-
imran.fanaswala
-
Jay Sprenkle