summaryrefslogtreecommitdiffstats
path: root/examples/network/blockingfortuneclient/blockingclient.cpp
diff options
context:
space:
mode:
authorPeter Hartmann <peter.hartmann@nokia.com>2009-11-09 14:40:18 (GMT)
committerPeter Hartmann <peter.hartmann@nokia.com>2009-11-09 14:58:26 (GMT)
commitbbdf3d9a600acfcb6df19eb5317fcb79da79c2df (patch)
tree29c5610f2b6b335bd233c6a06f6bbd9093a3a163 /examples/network/blockingfortuneclient/blockingclient.cpp
parent88b713cb11bdbded762c4004aae87aa9524d73cf (diff)
downloadQt-bbdf3d9a600acfcb6df19eb5317fcb79da79c2df.zip
Qt-bbdf3d9a600acfcb6df19eb5317fcb79da79c2df.tar.gz
Qt-bbdf3d9a600acfcb6df19eb5317fcb79da79c2df.tar.bz2
network examples: unify the fortune server/client examples
always use the first non-localhost IP address to listen/connect to, if there is one. Reviewed-by: Markus Goetz
Diffstat (limited to 'examples/network/blockingfortuneclient/blockingclient.cpp')
-rw-r--r--examples/network/blockingfortuneclient/blockingclient.cpp17
1 files changed, 16 insertions, 1 deletions
diff --git a/examples/network/blockingfortuneclient/blockingclient.cpp b/examples/network/blockingfortuneclient/blockingclient.cpp
index b731f7a..a3d7c23 100644
--- a/examples/network/blockingfortuneclient/blockingclient.cpp
+++ b/examples/network/blockingfortuneclient/blockingclient.cpp
@@ -50,7 +50,22 @@ BlockingClient::BlockingClient(QWidget *parent)
hostLabel = new QLabel(tr("&Server name:"));
portLabel = new QLabel(tr("S&erver port:"));
- hostLineEdit = new QLineEdit("Localhost");
+ // find out which IP to connect to
+ QString ipAddress;
+ QList<QHostAddress> ipAddressesList = QNetworkInterface::allAddresses();
+ // use the first non-localhost IPv4 address
+ for (int i = 0; i < ipAddressesList.size(); ++i) {
+ if (ipAddressesList.at(i) != QHostAddress::LocalHost &&
+ ipAddressesList.at(i).toIPv4Address()) {
+ ipAddress = ipAddressesList.at(i).toString();
+ break;
+ }
+ }
+ // if we did not find one, use IPv4 localhost
+ if (ipAddress.isEmpty())
+ ipAddress = QHostAddress(QHostAddress::LocalHost).toString();
+
+ hostLineEdit = new QLineEdit(ipAddress);
portLineEdit = new QLineEdit;
portLineEdit->setValidator(new QIntValidator(1, 65535, this));