在c++ (Qt)中设置一个非本地IP地址的服务器
Setting up a server with a non-local IP address in C++ (Qt)
我是网络编程的新手,所以我不确定这真的是正确的问题,但我想用c++制作一个服务器程序(我使用Qt.)我使用了"财富服务器"的例子,它创建了一个简单的服务器,您可以通过输入服务器显示的IP地址与客户端连接。如果我输入localhost(127.0.0.1),我可以连接,但是服务器说它运行的地址是169.254.253.67,我发现这是另一个本地地址(我也无法连接到它)。我如何运行服务器,使其不在本地地址上?
下面是我用的例子:http://doc.qt.digia.com/qt/network-fortuneserver.html服务器实现获得所有发现的ip地址的列表,然后找到第一个非本地ip。否则,默认使用localhost。如果您看到的是本地主机地址,则表明您的网络中没有正确设置,Qt无法确定LAN ip。
正如@drescherjm在评论中建议的那样,您可以通过0.0.0.0
显式侦听所有接口。要查看实际效果,您只需要在它使用ipAddress
字符串设置状态标签之前添加一行:
// add this to force the socket to listen on all interfaces
ipAddress = QHostAddress("0.0.0.0").toString();
// or if you have a local static ip and want to be explicit
// ipAddress = QHostAddress("192.168.xxx.xxx").toString();
// followed by the already existing line for setting the text
statusLabel->setText(tr("The server is running onnnIP: %1nport: %2nn"
"Run the Fortune Client example now.")
.arg(ipAddress).arg(tcpServer->serverPort()));
现在,如果你的网络设置正确,任何其他可以看到运行服务器应用程序的计算机都应该能够在给定的端口上连接。
相关文章:
- 如何使用 Boost Asio 在 Android 上获取我的本地 udp IP 地址?
- C++ / Qt:如何检测主机名或IP地址何时引用当前系统?
- 使用 C++将 IP 地址附加到字符*
- 使用全局 IP 地址时,C++ winsock 2 应用程序中的代码是否必须更改?
- Rabbitmq 将本地主机更改为 IP 地址C++
- C++ - 使用代码块从文件中读取 IP 地址
- C 使用from_string创建IP地址的C 导致lambda错误
- 如何在解析过程中区分 IP 地址和域名
- 如何使用 asio 库获取 IP 地址的主机名
- IP地址匹配过滤功能
- 如何将C++ ulong 4 字节 IP 地址正确转换为 C# 类型
- 如何在NS3中设置节点的IP地址和数据?
- 如何从活动的 udp 连接获取远程 IP 地址和端口
- 无符号的 int 到 IP 地址字符串,不带 itoa/to_string/boost
- 如何在FD_SET内获取套接字的IP地址
- 使用Boost库中的PC中获取我的以太网设备IP地址的列表
- 使用静态IP地址时,ESP32 httpclient连接拒绝了
- C++检查有效的 IP 地址或 IP
- 如何使用 Poco::Net::HTTPSClientSession 绑定特定的源 IP 地址
- 如何在Windows上绕过主机文件获取真实的IP地址