void connections::start_acceptor(){
auto socket=std::make_shared<boost::asio::ip::tcp::socket>(ioc);
con_acceptor.async_accept(*socket,[this,socket](const boost::system::error_code ec){
if(!ec){
this->status=true;
if(socket->is_open()){
cout<<"client connection Acknoledged with ip - "<<socket->remote_endpoint()<<endl;
boost::asio::spawn(con_acceptor.get_executor(), [this,socket](boost::asio::yield_context yield) {
r_w_handler(socket, yield);
});
};
this->start_acceptor();
} else{
cout<<"Error Accepting - "<<ec.message()<<endl;
this->status=false;
this->start_listener();
}
});
};
void connections::r_w_handler(std::shared_ptr<boost::asio::ip::tcp::socket> socket, boost::asio::yield_context yield) {
boost::beast::tcp_stream stream_socket(*socket);
try {
char data[128];
for (;;) {
// std::size_t length = socket->async_read_some(boost::asio::buffer(data), yield);
// boost::asio::async_write(*socket, boost::asio::buffer(data, length), yield);
}
} catch (const std::exception& e) {
std::cerr <<" Error with session " << e.what() << std::endl;
}
};
编译后出现错误 错误:使用已删除的函数
boost::asio::basic_stream_socket<Protocol, Executor>::basic_stream_socket(const boost::asio::basic_stream_socket<Protocol, Executor>&) [with Protocol = boost::asio::ip::tcp; Executor = boost::asio::any_io_executor]
该消息并未表明套接字已删除。
这意味着您正在尝试复制不可复制的类型。
复制发生在这里:
beast::tcp_stream stream_socket(*socket);
具体而言,由于
*socket
。如果你成功了
beast::tcp_stream stream_socket(std::move(*socket));
特定错误消失。
住在科里鲁
#include <boost/asio.hpp>
#include <boost/asio/spawn.hpp>
#include <boost/beast.hpp>
namespace asio = boost::asio;
namespace beast = boost::beast;
using tcp = asio::ip::tcp;
#include <iostream>
struct connections {
connections(asio::io_context& ioc, tcp::endpoint endpoint);
void start_listener();
void acceptLoop();
void r_w_handler(std::shared_ptr<tcp::socket> socket, asio::yield_context yield);
asio::io_context& ioc_;
tcp::acceptor acc_;
bool status_;
};
void connections::acceptLoop() {
auto socket = std::make_shared<tcp::socket>(ioc_);
acc_.async_accept(*socket, [this, socket](boost::system::error_code const ec) {
if (!ec) {
status_ = true;
if (socket->is_open()) {
std::cout << "client connection Acknoledged with ip - " << socket->remote_endpoint()
<< std::endl;
asio::spawn(
acc_.get_executor(),
[this, socket](asio::yield_context yield) { r_w_handler(socket, yield); },
asio::detached);
};
acceptLoop();
} else {
std::cout << "Error Accepting - " << ec.message() << std::endl;
status_ = false;
start_listener();
}
});
};
void connections::r_w_handler(std::shared_ptr<asio::ip::tcp::socket> socket, asio::yield_context yield) {
beast::tcp_stream stream_socket(std::move(*socket));
try {
char data[128];
for (;;) {
std::size_t length = socket->async_read_some(asio::buffer(data), yield);
asio::async_write(*socket, asio::buffer(data, length), yield);
}
} catch (std::exception const& e) {
std::cerr << " Error with session " << e.what() << std::endl;
}
};
connections::connections(asio::io_context& ioc, tcp::endpoint endpoint)
: ioc_(ioc)
, acc_(ioc, endpoint)
, status_(false) {
}
void connections::start_listener() {
acc_.listen();
acceptLoop();
}
int main() {
asio::io_context ioc;
tcp::endpoint endpoint(tcp::v4(), 8989);
connections conn(ioc, endpoint);
conn.start_listener();
ioc.run();
}
请注意,首先对
make_shared
套接字不再有多大意义。