简体   繁体   English

boost asio TCP服务器必须绑定到IP地址吗?

[英]boost asio TCP server must bind to an IP address?

I am not sure whether I am using BOOST ASIO properly, my code is as follows, 我不确定我是否正确使用BOOST ASIO,我的代码如下,

  boost::asio::ip::tcp::resolver resolver(io_);
  boost::asio::ip::tcp::resolver::query query(std::string("127.0.0.1"), port);
  boost::asio::ip::tcp::endpoint endpoint = *resolver.resolve(query);
  acceptor_.open(endpoint.protocol());

It binds to 127.0.0.1. 它绑定到127.0.0.1。 When a client is connecting externally, the client is using the IP address 192.168.0.107 or something similar. 当客户端从外部连接时,客户端使用IP地址192.168.0.107或类似的东西。 The server implemented by BOOST ASIO just simply refuses the client connection. BOOST ASIO实现的服务器只是拒绝客户端连接。

So is there a way to deal with this? 那么有办法解决这个问题吗? I think for apache2, the apache2 is not required to bind to a specific IP, any client requests from any server interface (provided there are multiple interfaces) can connect to apache2. 我认为对于apache2,apache2不需要绑定到特定的IP,任何服务器接口的任何客户端请求(假设有多个接口)都可以连接到apache2。

Is there a way to solve it? 有办法解决吗? Or there's no way and I have to fix the server external IP? 或者没有办法,我必须修复服务器外部IP?

Thanks. 谢谢。

Bind to 0.0.0.0 . 绑定到0.0.0.0 That's the "wildcard" that listens on all interfaces for any incoming connection. 这是侦听任何传入连接的所有接口的“通配符”。

If you bind to 127.0.0.1 , your server will only accept incoming connections over the loopback, which won't let you client connect (since your client isn't using the loopback). 如果绑定到127.0.0.1 ,则服务器将仅通过环回接受传入连接,这将不允许客户端连接(因为您的客户端未使用环回)。

您可以创建一个端点来侦听任何IP,如下所示:

boost::asio::ip::tcp::endpoint ep(boost::asio::ip::tcp::v4(), listen_port );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM