问题
I am trying to write a simple server using Boost.Asio library. I want my server to receive a message from the client and print that message on the console. Here is the code of my server program:
#include <iostream>
#include <string>
#include <memory>
#include <boost/asio.hpp>
using namespace boost::asio;
using namespace boost::system;
using boost::asio::ip::tcp;
class Session : public std::enable_shared_from_this<Session> {
public:
Session(tcp::socket socket);
void start();
private:
tcp::socket socket_;
std::string data_;
};
Session::Session(tcp::socket socket) : socket_(std::move(socket))
{}
void Session::start()
{
socket_.async_read_some(buffer(data_), [this](error_code errorCode, size_t length) {
if (!errorCode) {
std::cout << "received: " << data_ << std::endl;
}
start();
});
}
class Server {
public:
Server(io_context& context);
private:
tcp::acceptor acceptor_;
void accept();
};
Server::Server(io_context& context) : acceptor_(context, tcp::endpoint(tcp::v4(), 8888))
{
accept();
}
void Server::accept()
{
acceptor_.async_accept([this](error_code errorCode, tcp::socket socket) {
if (!errorCode) {
std::make_unique<Session>(std::move(socket))->start();
}
accept();
});
}
int main()
{
boost::asio::io_context context;
Server server(context);
context.run();
return 0;
}
And here is the code of my client program:
#include <iostream>
#include <string>
#include <boost/asio.hpp>
using namespace boost::asio;
using boost::asio::ip::tcp;
int main()
{
io_context context;
tcp::socket socket(context);
tcp::resolver resolver(context);
connect(socket, resolver.resolve("127.0.0.1", "8888"));
while (true) {
try {
std::string data;
std::cin >> data;
write(socket, buffer(data));
} catch (const std::exception& exception) {
std::cerr << exception.what() << std::endl;
}
}
return 0;
}
But when I start the client, the server throws exception "read access violation". What do I do wrong?
回答1:
You're using enable_shared_from_this
and but nothing keeps your Session alive because you only use unique_ptr<Session>
.
This means your Session goes away during running operations.
Fix it:
std::make_shared<Session>(std::move(socket))->start();
Next, hold a shared pointer in your completion handler:
void Session::start()
{
auto self = shared_from_this();
socket_.async_read_some(buffer(data_), [this, self](error_code errorCode, size_t /*length*/) {
if (!errorCode) {
std::cout << "received: " << data_ << std::endl;
}
start();
});
}
Next, CUT the async loop if there is an error (or your session will loop infinitely):
socket_.async_read_some(buffer(data_), [this, self](error_code errorCode, size_t length) {
if (!errorCode && length) {
std::cout << "received: " << data_ << std::endl;
start();
}
});
Finally, resize the buffer so you can actually receive data (!):
data_.resize(32);
socket_.async_read_some(buffer(data_), [this, self](error_code errorCode, size_t length) {
if (!errorCode) {
data_.resize(length);
std::cout << "received: '" << data_ << "'" << std::endl;
start();
}
});
There are still some issues left, but hey, the program won't crash immediately and you have some results.
Update
Added a live demo showing some more suggestions Live On Coliru
#include <iostream>
#include <string>
#include <memory>
#include <boost/asio.hpp>
using namespace boost::asio;
using namespace boost::system;
using boost::asio::ip::tcp;
class Session : public std::enable_shared_from_this<Session> {
public:
Session(tcp::socket socket);
void start();
private:
tcp::socket socket_;
boost::asio::streambuf _sb;
};
Session::Session(tcp::socket socket) : socket_(std::move(socket))
{}
void Session::start()
{
auto self = shared_from_this();
async_read_until(socket_, _sb, '\n', [this, self](error_code errorCode, size_t /*length*/) {
std::cout << "completion " << errorCode.message() << "\n";
if (!errorCode) {
std::string line;
{
std::istream is(&_sb);
if (getline(is, line)) {
std::cout << "received: '" << line << "'" << std::endl;
}
start();
}
}
});
}
class Server {
public:
Server(io_context& context);
private:
tcp::acceptor acceptor_;
void accept();
};
Server::Server(io_context& context) : acceptor_(context, tcp::endpoint(tcp::v4(), 8888))
{
accept();
}
void Server::accept()
{
acceptor_.async_accept([this](error_code errorCode, tcp::socket socket) {
if (!errorCode) {
std::make_shared<Session>(std::move(socket))->start();
}
accept();
});
}
int main(int argc, char**) {
if (argc>1) {
io_context context;
tcp::socket socket(context);
tcp::resolver resolver(context);
connect(socket, resolver.resolve("127.0.0.1", "8888"));
std::string data;
while (getline(std::cin, data)) {
try {
data += '\n';
write(socket, buffer(data));
} catch (const std::exception& exception) {
std::cerr << exception.what() << std::endl;
}
}
} else {
boost::asio::io_context context;
Server server(context);
context.run();
}
}
来源:https://stackoverflow.com/questions/51028715/simple-server-using-boost-asio-throws-an-exception