服務器實現獲取所有已發現的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 on\n\nIP: %1\nport: %2\n\n"
"Run the Fortune Client example now.")
.arg(ipAddress).arg(tcpServer->serverPort()));
現在,如果你的網絡設置正確,可以看到一個任何其他計算機運行服務器應用程序應該能夠連接到給定的端口。
來源
2013-01-08 01:12:32
jdi
我不完全確定你在問什麼,但是如果你使用0.0.0.0作爲監聽地址,服務器將監聽你PC中的所有nics。或者你可以指定外部ipaddress給listen()調用。 – drescherjm
您可以使用以下鏈接來確定ipaddress是哪個主nic:http://qt-project.org/forums/viewthread/1439 – drescherjm
@drescherjm您無法指定外部IP地址。 – EJP