gpt4 book ai didi

c++ - boost::asio 和同步读取导致编译错误

转载 作者:行者123 更新时间:2023-11-28 04:03:35 38 4
gpt4 key购买 nike

我正在尝试让客户端和服务器进行通信,并且正在通信的数据是一个结构 vector

std::vector<element> elemvec

它是类的私有(private)成员,但我们有方法使用 bloom_db.getelem(unsigned int) 检索该 vector 的各个元素.

在 boost 库代码内部编译肯定是失败的。

我知道我在流、缓冲区和序列化方面的知识存在很大缺陷,但似乎我需要一些帮助,因为我不想从 boost 示例中复制代码。

以下是我的代码:

typedef struct elem{        //This will be seen as a POD (Plain Old Data) in boost::asio::buffer
bool bit;
int count;
uint64_t hashSum[2];
uint64_t idSum;

template <typename Archive>
void serialize(Archive &ar, const unsigned int version){
ar & bit;
ar & count;
ar & hashSum[0];ar & hashSum[1];
ar & idSum;
}
}element;

void sendData(tcp::socket& socket, boost::asio::const_buffer& data)
{
boost::asio::write(socket,boost::asio::buffer(data));
}


void getData(tcp::socket& socket, boost::asio::mutable_buffer& data)
{
boost::asio::read(socket, data);
}


void netcom_client(int portn, string serverip, bloom_filter& bloom_db){
io_service io_service;
ip::tcp::socket client_socket(io_service);
boost::asio::const_buffer snd_buf;
boost::system::error_code ec;
element snd_elem;
string bufdata;
std::ostringstream os;
boost::archive::text_oarchive out_archive {os}; //archive is connected to global stringstream object
unsigned int i;

client_socket.connect(tcp::endpoint(address::from_string(serverip),portn));
//client sends the data : this loop is to send the data
for(i=0; i< bloom_db.size(); ++i){

::set_elemvec(snd_elem,bloom_db.getelem(i));
out_archive << snd_elem; //element inserted in archive
bufdata = os.str();
snd_buf = boost::asio::buffer(os.str());
sendData(client_socket,snd_buf);
}

cout<<"Sent "<<i<<" elements from client"<<endl<<std::flush;

}

void netcom_server(int portn, bloom_filter& bloom_db){
io_service io_service;
element recv_elem;
char bufdata[sizeof(element)];
boost::system::error_code ec;
bloom_filter recvd_bloomdb(4,"rcvbloom_db",4096);
std::istringstream is(bufdata);
boost::archive::text_iarchive in_archive {is};
unsigned int i;

ip::tcp::socket server_socket(io_service);
tcp::acceptor acceptor_server(io_service,tcp::endpoint(tcp::v4(), portn));
acceptor_server.accept(server_socket);

for(i=0; i<recvd_bloomdb.size(); ++i){
boost::asio::mutable_buffer rcv_buf = boost::asio::buffer((void*)bufdata,sizeof(element));
getData(server_socket, rcv_buf);
in_archive >> recv_elem;
recvd_bloomdb.tailadd_elem(recv_elem);
}
cout<<"Received "<<i<<" elements at server"<<endl<<std::flush;

}

First Line from Error

/usr/include/boost/asio/detail/consuming_buffers.hpp:在“class boost::asio::detail::consuming_buffers<boost::asio::mutable_buffer, boost::asio::mutable_buffer>’:”的实例化中/usr/include/boost/asio/impl/read.hpp:45:44:需要来自 ‘std::size_t boost::asio::read(SyncReadStream&, const MutableBufferSequence&, CompletionCondition, boost::system::error_code&) [with SyncReadStream = boost::asio::basic_stream_socket<boost::asio::ip::tcp>; MutableBufferSequence = boost::asio::mutable_buffer; CompletionCondition = boost::asio::detail::transfer_all_t; std::size_t = long unsigned int]’/usr/include/boost/asio/impl/read.hpp:64:39:需要来自 ‘std::size_t boost::asio::read(SyncReadStream&, const MutableBufferSequence&) [with SyncReadStream = boost::asio::basic_stream_socket<boost::asio::ip::tcp>; MutableBufferSequence = boost::asio::mutable_buffer; std::size_t = long unsigned int]’ibflt.cpp:309:35: 从这里需要/usr/include/boost/asio/detail/consuming_buffers.hpp:164:5: 错误:

中没有名为“const_iterator”的类型
‘class boost::asio::mutable_buffer’
const_iterator;

/usr/include/boost/asio/detail/consuming_buffers.hpp:261:36: 错误:在

中没有名为“const_iterator”的类型
‘class boost::asio::mutable_buffer’
typename Buffers::const_iterator begin_remainder_;

最佳答案

boost::asio::read 作为第二个参数 buffer 必须满足 Mutable Buffer Sequence requirements ,在 1.65 类 mutable_buffers_1 中执行此操作,而不是 mutable_buffer (它将从 1.66 版本开始工作)。

所以改变:

void getData(tcp::socket& socket, boost::asio::mutable_buffers_1& data)
{
boost::asio::read(socket, data);
}

boost::asio::mutable_buffers_1 rcv_buf = ...

    std::ostringstream os;
//...
snd_buf = boost::asio::buffer(os.str()); // [1]
sendData(client_socket,snd_buf);

上面的代码不能工作,我的意思是它编译得很好,但它会给你带来未定义的行为。为什么?

boost::buffer 不会复制传递的数据。它只返回元组(指向数据和数据大小的指针),不复制底层数据,buffer 只是包装它。

ostringstream::str() 通过 COPY 返回字符串,reference .所以缓冲区接受它,包装成元组,并在完整表达式的末尾销毁这个临时字符串,因此你在 buffer 返回的元组中有悬空引用。

解决方案?

创建命名字符串:

std::string str = os.str();
snd_buf = boost::asio::buffer(str);

关于c++ - boost::asio 和同步读取导致编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59126560/

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