操作系统:linux 64位ARCH。
提升:1.46.1
编译器:clang ++ / GCC。
我有一个代码片段,其中包含一个以boost :: asio为例(Chat Server)建模的tcp接受器。但是,当我运行片段时,没有监听TCP套接字出现在netstat监听(linux)中。但是,聊天服务器示例在编译时会显示。有人可以指出我做错了什么吗?
#include <boost/asio.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/bind.hpp>
#include <list>
#include <iostream>
using namespace boost::asio;
using namespace boost::asio::ip;
class ClientConnection
{
public:
ClientConnection(io_service & io_s)
: m_socket(io_s) {}
tcp::socket & socket() { return m_socket; }
private:
tcp::socket m_socket;
};
typedef boost::shared_ptr<ClientConnection> client_connection_ptr;
class ClientConnectionAcceptor
{
public:
ClientConnectionAcceptor(unsigned short port)
: m_io_service(),
m_port(port),
m_endpoint(tcp::v4(), m_port),
m_acceptor(m_io_service, m_endpoint)
{
std::cout << "acceptor is open : " << m_acceptor.is_open() << std::endl;
client_connection_ptr ccp(new ClientConnection(m_io_service));
m_acceptor.async_accept(
ccp->socket(),
boost::bind(&ClientConnectionAcceptor::handle_accept,this,
ccp, placeholders::error));
}
void handle_accept(client_connection_ptr ccp, const boost::system::error_code & error)
{
std::cout << "in handle_accept" << std::endl;
if(!error)
{
// m_rpc_oracle.AddNewClient(ccp);
client_connection_ptr new_ccp(new ClientConnection(m_io_service));
m_acceptor.async_accept(
new_ccp->socket(),
boost::bind(&ClientConnectionAcceptor::handle_accept,this,
ccp, placeholders::error));
}
}
io_service & io_service() { return m_io_service; }
private:
boost::asio::io_service m_io_service;
tcp::endpoint m_endpoint;
tcp::acceptor m_acceptor;
unsigned short m_port;
};
int main()
{
ClientConnectionAcceptor acceptor(5000);
acceptor.io_service().run();
}
答案 0 :(得分:2)
我发现如果我将endpoint
和acceptor
更改为共享指针,我可以在我的Windows机器上运行它,而不是通过在构造函数中将它们作为参数传递来创建它们,我专门在构造函数中创建了共享指针。我不确定为什么会这样。我唯一的猜测是,也许不能保证构造函数参数按它们出现的顺序传递或创建,因此您可能会尝试使用未正确初始化的acceptor
创建endpoint
然而?这真是我唯一的猜测。如果这对您有用,请告诉我。我可以通过端口localhost
上的5000
成功连接。
如果没有这些更改,我尝试通过localhost
连接的客户端告诉我连接被主动拒绝。然而,这种安排是成功的,并且似乎与原始代码的偏差尽可能小。希望它有所帮助。
#include <boost/asio.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/bind.hpp>
#include <list>
#include <iostream>
using namespace boost::asio;
using namespace boost::asio::ip;
class ClientConnection
{
public:
ClientConnection(io_service & io_s)
: m_socket(io_s) {}
tcp::socket & socket() { return m_socket; }
private:
tcp::socket m_socket;
};
typedef boost::shared_ptr<ClientConnection> client_connection_ptr;
class ClientConnectionAcceptor
{
public:
ClientConnectionAcceptor(unsigned short port)
: m_io_service(),
m_port(port)
{
// now initializing endpoint and acceptor as shared pointers inside the constructor
m_endpoint = boost::shared_ptr<tcp::endpoint>(new tcp::endpoint(tcp::v4(), m_port));
m_acceptor = boost::shared_ptr<tcp::acceptor>(new tcp::acceptor(m_io_service, *m_endpoint));
std::cout << "acceptor is open : " << m_acceptor->is_open() << std::endl;
client_connection_ptr ccp(new ClientConnection(m_io_service));
m_acceptor->async_accept(
ccp->socket(),
boost::bind(&ClientConnectionAcceptor::handle_accept,this,
ccp, placeholders::error));
}
void handle_accept(client_connection_ptr ccp, const boost::system::error_code & error)
{
std::cout << "in handle_accept" << std::endl;
if(!error)
{
// m_rpc_oracle.AddNewClient(ccp);
client_connection_ptr new_ccp(new ClientConnection(m_io_service));
m_acceptor->async_accept(
new_ccp->socket(),
boost::bind(&ClientConnectionAcceptor::handle_accept,this,
ccp, placeholders::error));
}
}
io_service & io_service() { return m_io_service; }
private:
boost::asio::io_service m_io_service;
boost::shared_ptr<tcp::endpoint> m_endpoint;
boost::shared_ptr<tcp::acceptor> m_acceptor;
unsigned short m_port;
};
int main()
{
ClientConnectionAcceptor acceptor(5000);
acceptor.io_service().run();
}
编辑
经过一些进一步的调查,发现问题实际上与ClientConnectionAcceptor
类的初始化列表有关。在类定义中,成员m_port
在 m_endpoint
和m_acceptor
之后被声明为。因此,即使初始化程序列表似乎在创建endpoint
和acceptor
之前设置了端口号,实际上,端口值无效或已初始化直到 之后endpoint
和acceptor
已经创建。更改类定义以在m_port
和endpoint
之前声明成员acceptor
可以解决问题。
答案 1 :(得分:1)
编译代码时出错
async_accept.cc:56: error: declaration of ‘boost::asio::io_service& ClientConnectionAcceptor::io_service()’
/opt/local/include/boost/asio/io_service.hpp:186: error: changes meaning of ‘io_service’ from ‘class boost::asio::io_service’
改变
io_service & io_service() { return m_io_service; }
到
io_service & get_io_service() { return m_io_service; }
似乎解决了编译器故障。运行生成的二进制文件为我显示netstat -l -t
中的侦听套接字。