Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
how can i know that my LAN is working or not
-
@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
-
@jsulm
It didn't work.
I'm sure that the IP Address is the right one and i've ping the IP Address it's worked.
This is my code here
It always get in to the qDebug which print "failed to connect host".
Did i miss something?
-
@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.
-
@jsulm
It got some error when i during compiling.
It is my code hereIt is the error here
I just type the same thing up to my code.
-
@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
-
@jsulm
You mean change like this?
If not where i should add this on?I had tried it. and will show the error
"QAbstractSocket::SocketTimeoutError"And if i use
qDebug <<socket->errorString();
It will show this error "Socket operation timed out"
-
@victor-wang Yes, like this. Even better is http://doc.qt.io/qt-5/qiodevice.html#errorString
-
@jsulm
I'm using port 8888.I had tried it. and will show the error
"QAbstractSocket::SocketTimeoutError"And i if i use
qDebug<<socket->errorString();
It will show this error "Socket operation timed out"
So is that mean i had set the time too long?
-
@the_
My code is too long.
I'm using qtcreator via Xming.
So i can't copy my code, i just can type it.
And because of there are too many codes i have no time to type it.
That's why i using as picture.
-
@victor-wang said in how can i know that my LAN is working or not:
So is that mean i had set the time too long?
No, it means that it was not possible to connect in that time.
You're doing it wrong. Please read documentation (http://doc.qt.io/qt-5/qabstractsocket.html#connectToHost): hostName is a URL not IP.
If you want to use IP then use http://doc.qt.io/qt-5/qabstractsocket.html#connectToHost-1
-
@jsulm
Ok, i see.
Now i tried to connect to the IP 192.168.120.115 like hereAnd i print the error out shows "Connection refused"
Is that because this IP address need the password to enter it?If it is, what should i do to solve this problem?
-
@victor-wang You're still doing it wrong. Please read documentation - I posted the links.
If you want to use IP address you need to pass http://doc.qt.io/qt-5/qhostaddress.html as parameter not a string.
-
@jsulm
Is that mean i have to set address first like this?hostaddress->setAddress(192.168.120.115);
Then connect it like this?
socket->connectToHost(hostaddress,PT_lanHostPort);
Is that right?
-
@victor-wang Yes
socket->connectToHost(QHostAddress("192.168.120.115"), PT_lanHostPort);
Why do you allocate everything on the heap with new? hostaddress can be just a local variable:
QHostAddress hostaddress("192.168.120.115"); socket.connectToHost(hostaddress, PT_lanHostPort); // No need to allocate socket on the heap with new as well!
-
@jsulm
Sorry, i'm new in Qt.Please be patient to me :)
This is my code hereIt still print "connection refused".
Why will it refused to connect?
"192.168.120.115" this is an IP which is a server and i need password and user to login.
Is this a factor that cause the error?
-
@victor-wang It is not a Qt thing, it is C++.
It should actually work even if authentication is required. Is port number correct? Can you ping this IP?
-
@jsulm
I'm sure that i can ping this IP address!
But i don't know how to check the port number.
-
@victor-wang Well, you should know on which port your server is listening.
-
A socket connection to the server is always possible, even if you need a username and a password to authenticate for using its service.
In case you do not provide any login data or incorrect login data the server is still responding to your request.- What kind of service is running on this host?
- How do you usually connect to this host?
-
Is there any way that i can know the port it is listening?
@the_
It is linux on it.
i will connect it through PUTTY.
-
@victor-wang Check your server configuration. How, depends on what server it is (Apache, NGNX,...). Or ask the people who manage the server.
-
@jsulm
I've change the port and it success!
Thank you so much !
I think i need to practice more.
-
@victor-wang Good!
And don't forget to read documentation :-)
-
@jsulm You are a patience ninja and a Gentleman. Great to see still people like you in these forums!!!
-
@Julian-Guarin Thanks! :-)