Resolve host address via dns

This commit is contained in:
Carl Philipp Klemm 2026-06-09 08:46:51 +02:00
parent 5040a79a93
commit 49e1aa98bf
2 changed files with 8 additions and 2 deletions

View file

@ -3,6 +3,7 @@
#include<QJsonObject>
#include<QJsonArray>
#include<QMessageBox>
#include<QHostInfo>
#include "mqttclient.h"
#include "items/mqttitem.h"
@ -169,7 +170,7 @@ SecondaryMainObject::SecondaryMainObject(QString host, int port, QObject *parent
globalItems.registerItemSource(tcpClient);
connect(&globalSensors, &SensorStore::sensorChangedState, tcpClient, &TcpClient::sensorEvent);
if(!tcpClient->launch(QHostAddress(host), port))
if(!tcpClient->launch(QHostInfo::fromName(host).addresses()[0], port))
{
QMessageBox::critical(nullptr, "Error", "Could not connect to "+host+":"+QString::number(port));
QMetaObject::invokeMethod(this, [](){exit(1);}, Qt::QueuedConnection);

View file

@ -23,7 +23,12 @@ void TcpClient::sendJson(const QJsonObject& json)
bool TcpClient::launch(const QHostAddress &address, quint16 port)
{
socket->connectToHost(address, port);
return socket->waitForConnected(2000);
if(!socket->waitForConnected(4000))
{
qWarning()<<"Unable to connect to "<<address<<':'<<port<<' '<<socket->error();
return false;
}
return true;
}
void TcpClient::processIncomeingJson(const QByteArray& jsonbytes)