我遵循以下3个eth:
eth0 inet addr:10.105.159.146 Bcast:10.105.159.255 Mask:255.255.254.0 inet6 addr: fe80::a00:27ff:fe11:84e1/64 eth1 inet addr:192.168.56.102 Bcast:192.168.56.255 Mask:255.255.255.0 inet6 addr: fe80::a00:27ff:fe5e:8a5c/64 lo inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host
这是我的代码如下:
server::server(const std::string& address, const std::string& port): io_service_(),
signals_(io_service_),
acceptor_(io_service_)
{
signals_.add(SIGINT);
signals_.add(SIGTERM);
#if defined(SIGQUIT)
signals_.add(SIGQUIT);
#endif // defined(SIGQUIT)
signals_.async_wait(boost::bind(&server::handle_stop, this));
boost::asio::ip::tcp::resolver resolver(io_service_);
boost::asio::ip::tcp::resolver::query query(address, port);
boost::asio::ip::tcp::endpoint endpoint = *resolver.resolve(query);
acceptor_.open(endpoint.protocol());
acceptor_.set_option(boost::asio::ip::tcp::acceptor::reuse_address(true));
acceptor_.bind(endpoint);
acceptor_.listen();
start_accept();
}
我应该传递哪个地址
boost::asio::ip::tcp::resolver::query query(**address**, port);
这样客户端就可以使用ip 192.168.56.102或10.105.159.146进行连接
目前如果我正在运行
server s("127.0.0.1", "1025"); //lo
server s("192.168.56.102", "1025"); //eth1
server s("10.105.159.146", "1025"); //eth0
这一切都属于同一台机器,所以我应该如何运行我的服务器或我可以做什么改变,以便它可以从客户端访问上述任何IP请求。
答案 0 :(得分:0)
将地址设置为“0.0.0.0”以接受所有IPV4地址。或者你可以简单地替换:
boost::asio::ip::tcp::resolver resolver(io_service_);
boost::asio::ip::tcp::resolver::query query(address, port);
boost::asio::ip::tcp::endpoint endpoint = *resolver.resolve(query);
使用:
boost::asio::ip::tcp::endpoint endpoint(boost::asio::ip::tcp::v4(), port);
请注意,在上面一行中,端口是unsigned short
而不是string
。