From f289576105f2c560d23d91d4078b832bbd19dc70 Mon Sep 17 00:00:00 2001 From: Peter Hartmann Date: Thu, 1 Oct 2009 10:37:15 +0200 Subject: Threaded Fortune server example: fix displayed IP listen to the first non-local IPv4 address found, as in commit 5e3775ae4c5263a25e63868e8a3f16244e4dde02 Reviewed-by: Aleksandar Babic --- examples/network/fortuneserver/server.cpp | 2 +- examples/network/threadedfortuneserver/dialog.cpp | 15 +++++++++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/examples/network/fortuneserver/server.cpp b/examples/network/fortuneserver/server.cpp index 016fa78..52b7d61 100644 --- a/examples/network/fortuneserver/server.cpp +++ b/examples/network/fortuneserver/server.cpp @@ -74,7 +74,7 @@ Server::Server(QWidget *parent) // if we did not find one, use IPv4 localhost if (ipAddress.isEmpty()) ipAddress = QHostAddress(QHostAddress::LocalHost).toString(); - statusLabel->setText(tr("The server is running on\nIP: \n%1 port:\n%2\n" + 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())); //! [1] diff --git a/examples/network/threadedfortuneserver/dialog.cpp b/examples/network/threadedfortuneserver/dialog.cpp index 51ae0d3..b1ea395 100644 --- a/examples/network/threadedfortuneserver/dialog.cpp +++ b/examples/network/threadedfortuneserver/dialog.cpp @@ -62,9 +62,20 @@ Dialog::Dialog(QWidget *parent) return; } - statusLabel->setText(tr("The server is running on port %1.\n" + QString ipAddress; + QList 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(); + } + // if we did not find one, use IPv4 localhost + if (ipAddress.isEmpty()) + ipAddress = QHostAddress(QHostAddress::LocalHost).toString(); + statusLabel->setText(tr("The server is running on\n\nIP: %1\nport: %2\n\n" "Run the Fortune Client example now.") - .arg(server.serverPort())); + .arg(ipAddress).arg(server.serverPort())); connect(quitButton, SIGNAL(clicked()), this, SLOT(close())); -- cgit v0.12