summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJesus Fernandez <jesus.fernandez@qt.io>2018-07-19 15:20:19 +0200
committerJesus Fernandez <Jesus.Fernandez@qt.io>2018-07-19 15:28:32 +0000
commit419e7a17f6bd1eb23f7eaa0fd09f5f11292eb26f (patch)
tree530b56c27368c03792a6c858f850fb9d821034e9 /src
parentcccece0a61ddcf9c9e09fccbb55159492a3fef3c (diff)
Return the server port in listen()
It allows knowing the server port in case the user chooses to use a random port. In the previous implementation listen() was returning a boolean value making it impossible to know the port number. Change-Id: I73384188b3b2eb57816eb6c6c9a7ac1a511b7456 Reviewed-by: Mikhail Svetkin <mikhail.svetkin@qt.io> Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Diffstat (limited to 'src')
-rw-r--r--src/httpserver/qabstracthttpserver.cpp12
-rw-r--r--src/httpserver/qabstracthttpserver.h2
2 files changed, 8 insertions, 6 deletions
diff --git a/src/httpserver/qabstracthttpserver.cpp b/src/httpserver/qabstracthttpserver.cpp
index 575845f..5b7673f 100644
--- a/src/httpserver/qabstracthttpserver.cpp
+++ b/src/httpserver/qabstracthttpserver.cpp
@@ -156,17 +156,19 @@ QAbstractHttpServer::QAbstractHttpServer(QAbstractHttpServerPrivate &dd, QObject
/*!
Tries to bind a \c QTcpServer to \a address and \a port.
- Returns \c true upon success, false otherwise.
+ Returns the server port upon success, -1 otherwise.
*/
-bool QAbstractHttpServer::listen(const QHostAddress &address, quint16 port)
+int QAbstractHttpServer::listen(const QHostAddress &address, quint16 port)
{
auto tcpServer = new QTcpServer(this);
const auto listening = tcpServer->listen(address, port);
- if (listening)
+ if (listening) {
bind(tcpServer);
- else
+ return tcpServer->serverPort();
+ } else {
delete tcpServer;
- return listening;
+ return -1;
+ }
}
/*!
diff --git a/src/httpserver/qabstracthttpserver.h b/src/httpserver/qabstracthttpserver.h
index 6794489..101a65b 100644
--- a/src/httpserver/qabstracthttpserver.h
+++ b/src/httpserver/qabstracthttpserver.h
@@ -61,7 +61,7 @@ class Q_HTTPSERVER_EXPORT QAbstractHttpServer : public QObject
public:
QAbstractHttpServer(QObject *parent = nullptr);
- bool listen(const QHostAddress &address = QHostAddress::Any, quint16 port = 0);
+ int listen(const QHostAddress &address = QHostAddress::Any, quint16 port = 0);
void bind(QTcpServer *server = nullptr);
QVector<QTcpServer *> servers() const;