Is there a way to check availability local server with timeout in Qt?
-
Hello,
I have on my VirtualBox linux with samba server. I have connection only between server and pc with virtualBox ( server can't connect with internet ). Is there a way to check availability server and after 5 seconds say: "no"?
Something like:
checkAvailabilityServer("\\192.168.43.21", 5);
?
If no Qt, maybe Winapi c++?
-
-
@hskoglund Thank you. But I don't give one more important information: my host OS is Windows 10. Curl is linux command right?
-
Hello!
You can try to use
QTcpSocket
class. Check out theconnectToHost
method: (https://doc.qt.io/qt-5/qabstractsocket.html#connectToHost) . Also, feel free to check thewaitForConnected
method as well: https://doc.qt.io/qt-5/qabstractsocket.html#waitForConnectedExample:
bool checkServerConnection() { QTcpSocket connectionSocket; connectionSocket.connectToHost("192.168.43.21", 5); connectionSocket.waitForConnected(5000); // 5 seconnds if (connectionSocket.state() == QTcpSocket::UnconnectedState) { connectionSocket.close(); return false; } connectionSocket.close(); return true; }
Happy coding!
-
@Cobra91151 Thank you for answer. Tommorow I check it! :)
-
@hskoglund Thank you. But I don't give one more important information: my host OS is Windows 10. Curl is linux command right?
@TomNow99 said in Is there a way to check availability local server with timeout in Qt?:
@hskoglund Thank you. But I don't give one more important information: my host OS is Windows 10. Curl is linux command right?
It is available for
Windows
OS as well. You can check out this link for more details: https://curl.haxx.se/download.html -
@Cobra91151 @mrjj That QTcpSocket is very good. But what if I have situation that I don't want wait?
So I want only do:
QTcpSocket connectionSocket; connectionSocket.connectToHost("192.168.43.21", port); qDebug()<<connectionSocket.state();
I can no wait and always get "connected" when server is up and "notConnected" when server is down? This is only server on my virtualBox.
-
@Cobra91151 @mrjj That QTcpSocket is very good. But what if I have situation that I don't want wait?
So I want only do:
QTcpSocket connectionSocket; connectionSocket.connectToHost("192.168.43.21", port); qDebug()<<connectionSocket.state();
I can no wait and always get "connected" when server is up and "notConnected" when server is down? This is only server on my virtualBox.
-
@jsulm Thank you. But I have other problem: I want check when server is down and I can't connect. Something like:
QTcpSocket connectionSocket; connect(connectionSocket, SIGNAL(cannotConnect()), this, SLOT(cannotConnectSlot())); connectionSocket.connectToHost("192.168.43.21", port);
-
@jsulm Thank you. But I have other problem: I want check when server is down and I can't connect. Something like:
QTcpSocket connectionSocket; connect(connectionSocket, SIGNAL(cannotConnect()), this, SLOT(cannotConnectSlot())); connectionSocket.connectToHost("192.168.43.21", port);
-
@jsulm I think about that, but I have many Qtimers :D
EDIT:
I want something like:
Run my server. And in my application I want check connect to this server every 3 seconds. If I can't connect I want write qDebug()<<"noConnection" and end checking.
-
@jsulm I think about that, but I have many Qtimers :D
EDIT:
I want something like:
Run my server. And in my application I want check connect to this server every 3 seconds. If I can't connect I want write qDebug()<<"noConnection" and end checking.
-
@jsulm Hmmm. I tried add new Qthread and check connection in this Qthread, but I get error "Cannot create children for a parent that is in a different thread". My code:
myThreadClass::myThreadClass(QTcpSocket *connSocket): connectionSocket(connSocket) { } myThreadClass::~myThreadClass() { connectionSocket->close(); } void myThreadClass::run() { while(1) { connectionSocket->connectToHost("192.168.43.23", 139); connectionSocket->waitForConnected(3000); if(connectionSocket->state() == QTcpSocket::UnconnectedState) { connectionSocket->close(); emit noConnection(); } connectionSocket->close(); } }
In mainWindow i create myThread and started it. I add QTcpSocket pointer and get this error.
class myThreadClass: public QThread { Q_OBJECT public: myThreadClass(QTcpSocket *connSocket); ~myThreadClass(); void run(); private: QTcpSocket *connectionSocket; signals: void noConnection(); };
-
@jsulm Hmmm. I tried add new Qthread and check connection in this Qthread, but I get error "Cannot create children for a parent that is in a different thread". My code:
myThreadClass::myThreadClass(QTcpSocket *connSocket): connectionSocket(connSocket) { } myThreadClass::~myThreadClass() { connectionSocket->close(); } void myThreadClass::run() { while(1) { connectionSocket->connectToHost("192.168.43.23", 139); connectionSocket->waitForConnected(3000); if(connectionSocket->state() == QTcpSocket::UnconnectedState) { connectionSocket->close(); emit noConnection(); } connectionSocket->close(); } }
In mainWindow i create myThread and started it. I add QTcpSocket pointer and get this error.
class myThreadClass: public QThread { Q_OBJECT public: myThreadClass(QTcpSocket *connSocket); ~myThreadClass(); void run(); private: QTcpSocket *connectionSocket; signals: void noConnection(); };
This post is deleted! -
@jsulm Hmmm. I tried add new Qthread and check connection in this Qthread, but I get error "Cannot create children for a parent that is in a different thread". My code:
myThreadClass::myThreadClass(QTcpSocket *connSocket): connectionSocket(connSocket) { } myThreadClass::~myThreadClass() { connectionSocket->close(); } void myThreadClass::run() { while(1) { connectionSocket->connectToHost("192.168.43.23", 139); connectionSocket->waitForConnected(3000); if(connectionSocket->state() == QTcpSocket::UnconnectedState) { connectionSocket->close(); emit noConnection(); } connectionSocket->close(); } }
In mainWindow i create myThread and started it. I add QTcpSocket pointer and get this error.
class myThreadClass: public QThread { Q_OBJECT public: myThreadClass(QTcpSocket *connSocket); ~myThreadClass(); void run(); private: QTcpSocket *connectionSocket; signals: void noConnection(); };
@TomNow99 said in Is there a way to check availability local server with timeout in Qt?:
while(1)
You're blocking event loop...
And the socket must be allocated in the thread where you want to use it. -
@jsulm Hmmm, but how? Something like:
in QTimerSlot ( check evert 3 sec ) I have:
connectionSocket->connectToHost("192.168.43.23", 139); timer2.start(1000); // maybe singleShot
and how to connect information from slotConnected and other QTimer?
slotConnected() { result = true; } slotTimer2() { if(result == false ) { } }
?
-
@jsulm Hmmm, but how? Something like:
in QTimerSlot ( check evert 3 sec ) I have:
connectionSocket->connectToHost("192.168.43.23", 139); timer2.start(1000); // maybe singleShot
and how to connect information from slotConnected and other QTimer?
slotConnected() { result = true; } slotTimer2() { if(result == false ) { } }
?