因为TCP是流式处理的,所以包没有边界,必须设计一个包头,里面表示包的长度(一般用字节表示),根据这个来逐个拆包。如果对于发送/接收频率不高的话,一般也就不做拆包处理了,因为不大可能有粘包现象。

以下是粘包和拆包的分析:

http://blog.csdn.net/zhangxinrun/article/details/6721495

用Qt的TCPSocket读出的数据来拆:

http://www.aiuxian.com/article/p-1732805.html

我是根据以上链接例子Qt的逻辑来实现的,用Boost的ASIO来读取,是同步的:

 m_imp->m_thread = boost::make_shared<boost::thread>(
[=]()
{
while (!boost::this_thread::interruption_requested())
{
boost::this_thread::interruption_point(); try
{
boost::system::error_code ec;
std::vector<uint8_t> tmpreadBuffer( * ); size_t bytes_transferred = m_imp->m_sockPtr->read_some(boost::asio::buffer(tmpreadBuffer), ec); std::cout << "Byte Transfered:" << bytes_transferred << "\n"; if (!ec)
{ if (bytes_transferred == ) continue; if (bytes_transferred < MSG_HEAD_SIZE)
{
m_imp->m_readBuffer.insert(m_imp->m_readBuffer.end(), tmpreadBuffer.begin(), tmpreadBuffer.begin() + bytes_transferred / sizeof(uint8_t));
continue;
}
else
{
m_imp->m_readBuffer.insert(m_imp->m_readBuffer.end(), tmpreadBuffer.begin(), tmpreadBuffer.begin() + bytes_transferred / sizeof(uint8_t)); size_t totalSize = m_imp->m_readBuffer.size()*sizeof(uint8_t); while (totalSize)
{
size_t msgSize = m_imp->getMsgLen();
std::cout << "Msg Size is:" << msgSize << "\n"; std::vector<uint8_t>::const_iterator first = m_imp->m_readBuffer.begin();
std::vector<uint8_t>::const_iterator last = m_imp->m_readBuffer.begin() + msgSize / sizeof(uint8_t);
std::vector<uint8_t> tmpMsg(first, last); m_imp->m_msgQueue.push_back(tmpMsg); m_imp->m_readBuffer.erase(first, last); totalSize = m_imp->m_readBuffer.size()*sizeof(uint8_t); } } }
else
{
std::cerr << "recv error : RAC module!" << ec.message() << std::endl;
m_imp->m_sockPtr->close();
break;
}
}
catch (std::exception& e)
{
std::cerr << e.what() << std::endl;
m_imp->m_sockPtr->close();
break;
}
}
} );

以下一个附带干货,以前一直不太理解Qt的TCPSocket,下面是底层原理:

http://blog.csdn.net/ying_593254979/article/details/17006507

05-07 15:11