boost::asio::placeholders::bytes_transferred
在async_read_until()
中是什么意思?在回调函数中,它会返回比streambuf.size()
更小的值。在回调之前,streambuf
已经清楚。综上所述,... bytes_transferred
不是实际通过套接字的字节数,而是更少。我是否误解了所有这些,或者是什么?boost :: asio ::占位符:: bytes_transferred是什么意思
编辑:我从插座阅读以下协议:
Y43,72,0,,91009802000000603=0000000000000000000
"Y43,"
- 是首部。
"Y"
- 是消息类型。
"43"
- 要读取的附加字节
","
- 分隔符。标题是直到遇到第一个“,”。
我的代码是用于阅读是这样的:
void handle_write(const boost::system::error_code& error,
size_t bytes_transferred)
{
if (!error)
{
boost::asio::async_read_until(
socket_,
inputStreamBuffer_,
',',
boost::bind(
&client::handle_read1, this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred
)
);
}
else
{
std::cout << "Write failed: " << error << "\n";
}
}
void handle_read1(const boost::system::error_code& error,
size_t bytes_transferred)
{
cout << "bytes_transferred=" << bytes_transferred << endl;
if (!error)
{
cout << "0 size=" << inputStreamBuffer_.size() << endl;
istream is(&inputStreamBuffer_);
char c[1000];
is.read(c,bytes_transferred);
c[bytes_transferred]=0;
for (int i=0;i<bytes_transferred;++i)
{
cout << dec << "c[" << i << "]=" << c[i] << " hex=" << hex << static_cast<int>(c[i]) << "#" << endl;
}
}
else
{
std::cout << "Read failed: " << error << "\n";
}
}
对于来自对方发送数据流:
Y43,71,0,,91009802000000595=0000000000000000000
有些时候,我读了这一点:
bytes_transferred=4
0 size=47
c[0]=Y hex=59#
c[1]=4 hex=34#
c[2]=3 hex=33#
c[3]=, hex=2c#
对于从对方发送的流:
个
Y43,72,0,,91009802000000603=0000000000000000000
但其他时候,我读了这一点:
bytes_transferred=7
0 size=47
c[0]= hex=0#
c[1]= hex=0#
c[2]= hex=0#
c[3]= hex=0#
c[4]=7 hex=37#
c[5]=2 hex=32#
c[6]=, hex=2c#
插座固定用SSL和客户端和服务器应用程序稍微修改从boost_asio /示例/ SSL/*例子。
在第二个例子中,我失去了整个头部:(
感谢您的回复,但我认为我遇到了其他问题。 有时,直到第一个分隔符为止的缓冲区内容被清零,所以我有效地丢失了数据包的标题。不过,这是随机行为。 –