我试图在3台不同的计算机上运行我的基本服务器(2个在Windows和1个Linux上),只有我的计算机无法正常运行服务器。在一两个请求之后,boost asio抛出一个std :: bad_alloc。我查看了Valgrind:没有泄漏。 Linux运行我的代码完全是我的第二台计算机,但每次都失败了。我无法弄清楚我的电脑中是什么导致服务器崩溃。我试图禁用防病毒:不起作用。我做了一个防火墙例外。代码块的调试器发送给我:
#0 004F7980 __cxa_throw () (??:??)
#1 004F749E operator new(unsigned int) () (??:??)
#2 005CACB8 typeinfo for std::time_put<wchar_t, std::ostreambuf_iterator<wchar_t, std::char_traits<wchar_t> > > () (??:??)
#3 004F55F0 std::bad_alloc::what() const () (??:??)
#4 0AF17F94 ?? () (??:??)
#5 D0D0D0D0 ?? () (??:??)
#ifndef MSSID_SERVER_CORE_H_INCLUDED
#define MSSID_SERVER_CORE_H_INCLUDED
#include <cstdlib>
#include <iostream>
#include <memory>
#include <utility>
#include <boost/bind.hpp>
#include <boost/asio.hpp>
#include "XML/pugixml.hpp"
#include "XML/pugiconfig.hpp"
#include "Network/RQUtils.h"
#include <boost/asio.hpp>
using boost::asio::ip::tcp;
using namespace pugi;
int startServer();
class MSSID_Session
: public std::enable_shared_from_this<MSSID_Session>
{
public:
MSSID_Session(tcp::socket socket)
: socket_(std::move(socket)){};
~MSSID_Session();
void start();
private:
void start_routine();
void greetingsHandle_write(boost::system::error_code ec, std::size_t length);
void greetingsHandle_read(boost::system::error_code ec, std::size_t length);
void requestHandle_write(boost::system::error_code ec, std::size_t length);
void requestHandle_read(boost::system::error_code ec, std::size_t length);
void headerHandle_write(boost::system::error_code ec, std::size_t length);
void headerHandle_read(boost::system::error_code ec, std::size_t length);
void setError(boost::system::error_code ec);
boost::asio::streambuf sb;
int err;
string errmsg;
char data_[5];
tcp::socket socket_;
bool grtd;
int errc;
};
class MSSID_Server
{
public:
MSSID_Server(boost::asio::io_service& io_service, short port)
: acceptor_(io_service, tcp::endpoint(tcp::v4(), port)),
socket_(io_service)
{
start_accept();
}
private:
void start_accept();
tcp::acceptor acceptor_;
tcp::socket socket_;
};
#endif // MSSID_SERVER_CORE_H_INCLUDED
#include <cstdlib>
#include <iostream>
#include <memory>
#include <utility>
#include <boost/bind.hpp>
#include <boost/asio.hpp>
#include "MSSID_Server_Core.h"
#include <boost/thread/thread.hpp>
using boost::asio::ip::tcp;
MSSID_Session::~MSSID_Session()
{
std::cout << "Session ended !" << std::endl;
}
void MSSID_Session::start()
{
grtd = false;
std::cout << "Session started !" << std::endl;
start_routine();
}
void MSSID_Session::start_routine()
{
auto self(shared_from_this());
socket_.async_read_some(sb.prepare(5), boost::bind(&MSSID_Session::greetingsHandle_read,self,boost::asio::placeholders::error,boost::asio::placeholders::bytes_transferred));
}
void MSSID_Session::greetingsHandle_read(boost::system::error_code er, size_t len)
{
if(er)
this->setError(er);
auto self(shared_from_this());
if(len!=5)
{
err = -2;
errmsg = "[ERROR] Incorrect greetings, aborting socket";
socket_.close();
} else
{
sb.commit(5);
string greetings(boost::asio::buffers_begin(sb.data()), boost::asio::buffers_begin(sb.data()) + 5);
if(strcmp(greetings.c_str(),"MSSID")==0)
{
socket_.async_write_some(boost::asio::buffer("MSSID",5), boost::bind(&MSSID_Session::greetingsHandle_write,self,boost::asio::placeholders::error,boost::asio::placeholders::bytes_transferred));
}
}
cout << "Greetings read : Done" << endl;
}
void MSSID_Session::greetingsHandle_write(boost::system::error_code er, size_t len)
{
auto self(shared_from_this());
if(er)
this->setError(er);
if(len!=5)
{
err = -3;
errmsg = "[ERROR] Can't send greeting : " + er.message();
socket_.close();
}
boost::asio::async_read_until(socket_,sb,"</MSSID>",boost::bind(&MSSID_Session::headerHandle_read,self,boost::asio::placeholders::error,boost::asio::placeholders::bytes_transferred));
cout << "C'est bon " << endl;
}
void MSSID_Session::headerHandle_read(boost::system::error_code er, size_t len)
{
if(er)
this->setError(er);
sb.commit(len);
string header(boost::asio::buffers_begin(sb.data()), boost::asio::buffers_begin(sb.data()) + sb.size());
}
void MSSID_Server::start_accept()
{
acceptor_.async_accept(socket_,
[this](boost::system::error_code ec)
{
if (!ec)
{
std::make_shared<MSSID_Session>(std::move(socket_))->start();
}
start_accept();
});
}
int startServer()
{
try
{
boost::asio::io_service io_service;
boost::thread_group threadpool;
MSSID_Server s(io_service, 5553);
io_service.run();
}
catch (std::exception& e)
{
std::cerr << "Exception: " << e.what() << "\n";
}
return 0;
}
void MSSID_Session::setError(boost::system::error_code ec)
{
err = ec.value();
errmsg = ec.message();
}