gpt4 book ai didi

c++ - boost asio transfer_exactly 读取 0 字节

转载 作者:搜寻专家 更新时间:2023-10-31 02:10:21 27 4
gpt4 key购买 nike

<分区>

给定一个通过 tcp 传输文件内容的示例服务器 -

#include <vector>
#include <fstream>
#include <iostream>
#include <boost/asio.hpp>

using namespace boost::asio;
using namespace boost::system;
using namespace boost::asio::ip;

using std::cerr;
using std::endl;
using std::ifstream;
using std::vector;
using std::ios;
using stream_iter = std::istreambuf_iterator<char>;

int main()
{
ifstream input("movie.ogv", ios::in | ios::binary);
vector<uint8_t> stream((stream_iter(input)), stream_iter());

io_service ioservice;
tcp::acceptor acceptor(ioservice, tcp::endpoint(tcp::v4(), 8000));
tcp::socket socket(ioservice);
acceptor.accept(socket);

error_code ec;
write(socket, boost::asio::buffer(stream), ec);
if (ec) {
cerr << ec.message() << endl;
}
}

我编写了以下客户端来提取正在传输的文件的起始 header -

#include <vector>
#include <iostream>
#include <array>
#include <boost/asio.hpp>

using namespace boost::asio;
using namespace boost::system;
using namespace boost::asio::ip;

using std::cerr;
using std::endl;
using std::ifstream;
using std::vector;
using std::ios;
using stream_iter = std::istreambuf_iterator<char>;

int main()
{
int header_size_ = 30000;
error_code ec;
vector<char> headerBytesBuf;
headerBytesBuf.reserve(header_size_);
io_service ioservice;
tcp::socket socket(ioservice);
socket.connect(tcp::endpoint(tcp::v4(), 8000));
const auto bTransfer = read(socket, buffer(headerBytesBuf),
transfer_exactly(header_size_), ec);

if (ec) {
socket.close();
std::cerr << ec.message();
}

std::cout << headerBytesBuf.size();
}

相关代码是 - const auto bTransfer = read(socket, buffer(headerBytesBuf), transfer_exactly(header_size_), ec); 将 0 字节读入 vector ,但没有记录错误:/

将该行更改为 -

std::array<char, 128> buf;
size_t total_transfer = 0;

while(total_transfer < header_size_) {
size_t len = socket.read_some(boost::asio::buffer(buf));
total_transfer += len;
if (len <= 0) break;
}

total_transfer 报告 30800,我现在可以在循环中修剪并附加到我的 vector。但为什么第一种方法在这里不起作用?

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com