summaryrefslogtreecommitdiffstats
path: root/examples/network
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
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')
-rw-r--r--examples/network/blockingfortuneclient/blockingclient.cpp17
-rw-r--r--examples/network/fortuneclient/client.cpp4
-rw-r--r--examples/network/fortuneserver/server.cpp4
-rw-r--r--examples/network/threadedfortuneserver/dialog.cpp4
4 files changed, 25 insertions, 4 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));
diff --git a/examples/network/fortuneclient/client.cpp b/examples/network/fortuneclient/client.cpp
index ca5d40e..e043f78 100644
--- a/examples/network/fortuneclient/client.cpp
+++ b/examples/network/fortuneclient/client.cpp
@@ -62,8 +62,10 @@ Client::Client(QWidget *parent)
// 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())
+ ipAddressesList.at(i).toIPv4Address()) {
ipAddress = ipAddressesList.at(i).toString();
+ break;
+ }
}
// if we did not find one, use IPv4 localhost
if (ipAddress.isEmpty())
diff --git a/examples/network/fortuneserver/server.cpp b/examples/network/fortuneserver/server.cpp
index 52b7d61..06f6eeb 100644
--- a/examples/network/fortuneserver/server.cpp
+++ b/examples/network/fortuneserver/server.cpp
@@ -68,8 +68,10 @@ Server::Server(QWidget *parent)
// 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())
+ ipAddressesList.at(i).toIPv4Address()) {
ipAddress = ipAddressesList.at(i).toString();
+ break;
+ }
}
// if we did not find one, use IPv4 localhost
if (ipAddress.isEmpty())
diff --git a/examples/network/threadedfortuneserver/dialog.cpp b/examples/network/threadedfortuneserver/dialog.cpp
index b1ea395..d0d3fc0 100644
--- a/examples/network/threadedfortuneserver/dialog.cpp
+++ b/examples/network/threadedfortuneserver/dialog.cpp
@@ -67,8 +67,10 @@ Dialog::Dialog(QWidget *parent)
// 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())
+ ipAddressesList.at(i).toIPv4Address()) {
ipAddress = ipAddressesList.at(i).toString();
+ break;
+ }
}
// if we did not find one, use IPv4 localhost
if (ipAddress.isEmpty())