|
Boost Users : |
Subject: Re: [Boost-users] Very interesting problem with TCP sockets
From: Alex MDC (alex.mdc_at_[hidden])
Date: 2014-09-01 23:42:10
Hi Igor,
2014-09-01 16:55 GMT+10:00 Igor Mironchik <igor.mironchik_at_[hidden]>:
> And my question is: who is so bad guy, Boost or Qt?
>
I'm going to go out on a limb here and guess that the "bad guy" is neither
Boost nor Qt, but rather your own code.
Sure enough, having a look at your server code I can see at least two
problems:
void
Socket::start( std::shared_ptr< Socket > socket )
{
std::cout << "Socket::start" << std::endl;
char buf[ msgSize ];
boost::asio::async_read( m_socket,
boost::asio::buffer( buf, msgSize ),
[ this, socket, &buf ] ( boost::system::error_code error, std::size_t )
{
if( error )
m_server->disconnection( socket );
else
{
if( std::strncmp( buf, msg, msgSize ) != 0 )
{
std::cout << "Message is WRONG" << std::endl;
m_server->disconnection( socket );
}
else if( m_socket.is_open() )
{
std::cout << "Message is OK" << std::endl;
start( socket );
}
}
}
);
}
Firstly, buf is located on the stack, meaning it will go out of scope as
soon as Socket::start() returns. As you are using async_read() this will
happen immediately.
Secondly, you are ignoring the bytes_transferred parameter in your read
handler. This is the size_t parameter after error_code. There is no
guarantee that the buffer will be filled each time your read handler is
called, so you need to check this value to see how many bytes were actually
read into your buffer, otherwise you may encounter garbage bytes at the end.
As boost and Qt are both heavily used and very mature libraries, you will
make better progress initially if you take the frame of mind that any
problems you encounter are caused by your own code (unless you are _really_
unfortunate!)
Hope that helps.
Alex
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net