18 #include <QNetworkInterface>
47 connect(mSocket, &QTcpSocket::connected,
this, &SocketClientConnector::internalConnected);
48 connect(mSocket, &QTcpSocket::disconnected,
this, &SocketClientConnector::internalDisconnected);
60 mSocket->connectToHost(mInfo.
host, mInfo.
port);
71 if (mSocket->state() == QAbstractSocket::ConnectedState)
73 if (mSocket->state() == QAbstractSocket::UnconnectedState)
78 void SocketClientConnector::internalConnected()
84 void SocketClientConnector::internalDisconnected()
119 if (mSocket->state() == QAbstractSocket::ConnectedState)
121 if (mServer->isListening())
123 if (mSocket->state() == QAbstractSocket::UnconnectedState)
128 bool SocketServerConnector::startListen()
138 bool started = mServer->listen(QHostAddress::Any, mInfo.
port);
142 CX_LOG_INFO() << QString(
"Server address: %1").arg(this->getAllServerHostnames().join(
", "));
143 CX_LOG_INFO() << QString(
"Server is listening to port %1").arg(mServer->serverPort());
147 CX_LOG_INFO() << QString(
"Server failed to start. Error: %1").arg(mServer->errorString());
154 void SocketServerConnector::stopListen()
158 if (mServer && mServer->isListening())
160 CX_LOG_INFO() << QString(
"Server stopped listening to port %1").arg(mServer->serverPort());
166 void SocketServerConnector::incomingConnection(qintptr socketDescriptor)
170 if (this->mSocket->state() == QAbstractSocket::ConnectedState)
172 reportError(
"Incoming connection request rejected: The server can only handle a single connection.");
176 int success = mSocket->setSocketDescriptor(socketDescriptor, QAbstractSocket::ConnectedState);
177 QString clientName = mSocket->localAddress().toString();
183 QStringList SocketServerConnector::getAllServerHostnames()
185 QStringList addresses;
187 foreach(QNetworkInterface interface, QNetworkInterface::allInterfaces())
189 if (interface.flags().testFlag(QNetworkInterface::IsRunning))
190 foreach (QNetworkAddressEntry entry, interface.addressEntries())
192 if ( interface.hardwareAddress() !=
"00:00:00:00:00:00"
193 && entry.ip().toString() !=
"127.0.0.1"
194 && entry.ip().toString().contains(
".") )
195 addresses << QString(
"%1: %2").arg(interface.name()).arg(entry.ip().toString());