Solved how can i know that my LAN is working or not
-
Hi, I'm using Qt5.5 on my computer.
I wanna check if my LAN is working or not.
What the best solution that everybody suggest? -
There is nothing like LAN is working or not. You can check ipaddress exist for the interface or u can try connecting to some website using networkaccessmanager or try connecting to remote server. Based on this u can check the same.
-
@dheerendra
First, i wanna know is my network is working or not.
Second, I want to know if the address that i connect is valid or not.
If there are all true for my condition.
Then i will do other things for next step.For now
I had used this function like below to show my Lan interfaceProcess::executeProcessSync(QString("/sbin/ifconfig"),QStringList()<<lanName,message);
But it is not enough to prove that the address i want to connect is valid.
So i mean i need the solution that to check the address is the valid IP. -
@victor-wang What is a "valid" address/IP? You need to connect to it to be really sure it is valid and you can connect. There is no other way.
What does "working network" mean? That you are connected to a LAN? That you have access to the Internet? -
@jsulm
The meaning of "valid" is when i give this commend "ping 192.168.120.111" in my console, if it is a valid IP address it will return the speed of your internet back to you.
In the other hand, if it is not a "valid" IP address, it will not return anything.
The product i developed need to make sure that any possible that client might using invalid address and i have to make an error for them. -
@victor-wang Then try to connect to the server. If you succeed then everything is fine if not then there is no connection.
-
@jsulm
Do you have any function that you suggest?
I know to connect but i con't know which function can help me connect and if success will return true for example. -
@victor-wang Use QTcpSocket: http://doc.qt.io/qt-5/qabstractsocket.html#connectToHost
connectToHost will emit connected() signal when connected and error() if not. As alternative you can call http://doc.qt.io/qt-5/qabstractsocket.html#waitForConnected after calling connectToHost, but it will block until connected or timeout:socket->connectToHost("imap", 143); if (socket->waitForConnected(1000)) qDebug("Connected!");
-
To read the ipadress you can use qnetwork interface class. Also to check if ip is valid there is direct way. You need to connect to that ip using some port number. U need to find some server running on that port. In summary there is no direct way
-
-
@dheerendra
Do u have an example for me? -
@victor-wang
Is the timeout long enough to get response from the server? If not the connection will always fail -
@victor-wang What is the port you're using?
Also check what http://doc.qt.io/qt-5/qabstractsocket.html#error is returning.
From documentation http://doc.qt.io/qt-5/qabstractsocket.html#waitForConnected:
"Note: This function may fail randomly on Windows. Consider using the event loop and the connected() signal if your software will run on Windows."A note: there is no need to allocate socket on the heap, just use a local variable.
-
@the_
I had try to set the time to 5000.
But it still didn't work for me. -
-
@victor-wang
would be easier if you paste your code here and not on external sites as pictures ;) -
@victor-wang Error is not a signal it is just a method, call it if the connection doesn't succeed and print out its output:
qDebug() << socket->error();
Your code would be wrong even if error() would be a signal: you're trying to connect AFTER the connection failed. That means you would miss the signal.
-
@victor-wang Even better is http://doc.qt.io/qt-5/qiodevice.html#errorString
-
-
@victor-wang Yes, like this. Even better is http://doc.qt.io/qt-5/qiodevice.html#errorString