【发布时间】:2016-04-06 09:26:50
【问题描述】:
我试图弄清楚如何将 tcp 套接字限制为 localhost。我终于找到了可以编译的代码,但它不接受任何连接。
代码接受与endpoint_all的连接,但不接受用tcp::endpoint(ip::address::from_string("127.0.0.1"),port2);设置的“endpoint_local”变量;
boost::asio::io_service io_service;
short port = 9000;
tcp::endpoint endpoint_all = boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(),port);
tcp::endpoint endpoint_local = boost::asio::ip::tcp::endpoint(boost::asio::ip::address::from_string("127.0.0.1"),port);
try
{
server s(io_service, std::atoi("9000"),endpoint_all);
io_service.run();
}
catch (std::exception& e)
{
}
更新:我可以通过“telnet 127.0.0.1 9000”和“telnet localhost 9000”访问套接字。有问题的实际应用程序(PHP XDebug)不会连接到 ip 受限端点,但会以其他方式连接。
"telnet localhost 9000" 给出以下错误,但确实连接。我没有在 php.ini 中连接本地主机,但也许这条消息是相关的。 "连接被拒绝 ::1:"
我认为允许连接到 ::1: 是合适的,无论这是否是错误。
Trying ::1...
telnet: connect to address ::1: Connection refused
Trying fe80::1...
telnet: connect to address fe80::1: Connection refused
Trying 127.0.0.1...
Connected to localhost.
【问题讨论】: