QTcpSocket connecting even tho no server?
-
@SPlatten do you call
connectToHost
multiple times ? IIRC each connectTohost call may emit a HostFound signal -
@ollarch , the IP exists on the system, but there is no application running listening for connection, nothing is using that port.
@SPlatten said in QTcpSocket connecting even tho no server?:
@ollarch , the IP exists on the system, but there is no application running listening for connection, nothing is using that port.
So, the host is found.
As @KroMignon has told you a lot of times is to read the documentation.
It seems that you write some code, test, and if it fails you start writing a new thread in the forum.
Yes, I know that you have a lot work to do but this also applies to us. -
@SPlatten said in QTcpSocket connecting even tho no server?:
@ollarch , the IP exists on the system, but there is no application running listening for connection, nothing is using that port.
So, the host is found.
As @KroMignon has told you a lot of times is to read the documentation.
It seems that you write some code, test, and if it fails you start writing a new thread in the forum.
Yes, I know that you have a lot work to do but this also applies to us.@ollarch, I have read the documentation, and nowhere does it say that the hostFound signal is issue when the IP is found regardless of the port!
The IP and Port combine make the address, the IP itself is no indication that there is anything to connect to, not if there is nothing using the specified port. And this information is not documented, hence my question.
-
@SPlatten said in QTcpSocket connecting even tho no server?:
and nowhere does it say that the hostFound signal is issue when the IP is found regardless of the port!
Sorry but the error is called 'HostNotFound' - not 'Can not connect to host and port'...
-
@SPlatten said in QTcpSocket connecting even tho no server?:
From the Application Output I can see lots of Host Found! messages, is this normal when the IP:Port doesn't have anything listening to it?
Why do you never read documentation before asking what already describe in documentation?
Have you tried to readconnectToHost()
documentation (hint: just hit F1 key in Qt Creator):Attempts to make a connection to hostName on the given port. The protocol parameter can be used to specify which network protocol to use (eg. IPv4 or IPv6).
The socket is opened in the given openMode and first enters HostLookupState, then performs a host name lookup of hostName. If the lookup succeeds, hostFound() is emitted and QAbstractSocket enters ConnectingState. It then attempts to connect to the address or addresses returned by the lookup. Finally, if a connection is established, QAbstractSocket enters ConnectedState and emits connected().
At any point, the socket can emit errorOccurred() to signal that an error occurred.
hostName may be an IP address in string form (e.g., "43.195.83.32"), or it may be a host name (e.g., "example.com"). QAbstractSocket will do a lookup only if required. port is in native byte order.
As you could read:
- the socket is opened, so QTcpSocket::isOpen() will return true
- hostname lookup is started ==> hostFound() is emitted on success
- on success, connection is started
It is so hard to read documentation????
@KroMignon , I didn't say I never read the documentation, that's your inference and on this occasion I find that the documentation is not detailed enough. An address has two parts, the IP and the port.
-
@KroMignon , I didn't say I never read the documentation, that's your inference and on this occasion I find that the documentation is not detailed enough. An address has two parts, the IP and the port.
@SPlatten said in QTcpSocket connecting even tho no server?:
I find that the documentation is not detailed enough. An address has two parts, the IP and the port.
It is written in documentation:
The socket is opened in the given openMode and first enters HostLookupState, then performs a host name lookup of hostName. If the lookup succeeds, hostFound() is emitted=> host name lookup, this is done before connection starts, so no need of TCP port.
This is very explicitly described in documentation.
-
@ollarch, I have read the documentation, and nowhere does it say that the hostFound signal is issue when the IP is found regardless of the port!
The IP and Port combine make the address, the IP itself is no indication that there is anything to connect to, not if there is nothing using the specified port. And this information is not documented, hence my question.
@SPlatten
I don't want to get into an argument, but it does say that.https://doc.qt.io/qt-5/qabstractsocket.html#hostFound :
This signal is emitted after connectToHost() has been called and the host lookup has succeeded.
https://doc.qt.io/qt-5/qabstractsocket.html#connectToHost :
The socket is opened in the given openMode and first enters HostLookupState, then performs a host name lookup of hostName. If the lookup succeeds, hostFound() is emitted and QAbstractSocket enters ConnectingState. It then attempts to connect to the address or addresses returned by the lookup. Finally, if a connection is established, QAbstractSocket enters ConnectedState and emits connected().
So, as it says,
hostFound
signal is emitted on finding the host IP address. It does what it says on the packet, i.e. it's just a host lookup, by name or IP address, and tells you the host does exist.Connecting on the port comes after that, and of course may succeed or fail separately.
Anyway, there it is, that's how it works.
-
@SPlatten
I don't want to get into an argument, but it does say that.https://doc.qt.io/qt-5/qabstractsocket.html#hostFound :
This signal is emitted after connectToHost() has been called and the host lookup has succeeded.
https://doc.qt.io/qt-5/qabstractsocket.html#connectToHost :
The socket is opened in the given openMode and first enters HostLookupState, then performs a host name lookup of hostName. If the lookup succeeds, hostFound() is emitted and QAbstractSocket enters ConnectingState. It then attempts to connect to the address or addresses returned by the lookup. Finally, if a connection is established, QAbstractSocket enters ConnectedState and emits connected().
So, as it says,
hostFound
signal is emitted on finding the host IP address. It does what it says on the packet, i.e. it's just a host lookup, by name or IP address, and tells you the host does exist.Connecting on the port comes after that, and of course may succeed or fail separately.
Anyway, there it is, that's how it works.
@JonB , as I said previously the IP is just part of the address, the port completes the address, when you establish the connection the port is optional, but if supplied then it should be used because connecting to address and address:port are very different.
-
@JonB , as I said previously the IP is just part of the address, the port completes the address, when you establish the connection the port is optional, but if supplied then it should be used because connecting to address and address:port are very different.
@SPlatten said in QTcpSocket connecting even tho no server?:
address:port
This is wrong. It's host:port !
-
@SPlatten said in QTcpSocket connecting even tho no server?:
address:port
This is wrong. It's host:port !
@Christian-Ehrlicher , are you being piccy?
-
@SPlatten said in QTcpSocket connecting even tho no server?:
are you being piccy?
No, but your assumptions are wrong because of this wrong definition.
-
@JonB , as I said previously the IP is just part of the address, the port completes the address, when you establish the connection the port is optional, but if supplied then it should be used because connecting to address and address:port are very different.
@SPlatten
Sorry, Simon, you're incorrect. I did try to explain. I do know what IP addresses vs ports are. I was just pointing out, politely, that Qt'shostFound
is emitted on host found, not on port is listening/connected. Anyways, it's not worth arguing about, we were just talking about how Qt documents what it is that it does when. -
@JonB , as I said previously the IP is just part of the address, the port completes the address, when you establish the connection the port is optional, but if supplied then it should be used because connecting to address and address:port are very different.
@SPlatten said in QTcpSocket connecting even tho no server?:
as I said previously the IP is just part of the address, the port completes the address, when you establish the connection the port is optional, but if supplied then it should be used because connecting to address and address:port are very different.
Your are mixing up definitions:
- Hostname is IP address or a name (like "www.qt.com")
- portnum is the TCP port (number between 0 and 65534).
- combination of hostname and TCP port is an Endpoint
- a TCP connection is defined as a combination of 2 endpoints, the local endpoint and the remote endpoint
-
I have the same issue no listening server at the adress port but it's connecting anyway:
bool Client::bindTo(QString adress, quint16 port) { QHostAddress adressToCo; if (adressToCo.setAddress(adress) == true) { m_socket = new QTcpSocket; if (!m_socket->bind(adressToCo, port)) { m_errorMsg = "Cannot connect to " + adress; qDebug() << "Cannot connect to " + adress; } } else { m_errorMsg = "Wrong IP address."; qDebug() << "Wrong IP address: " + adress; return false; } qDebug() << "Bind corectly to " + adress; return true; } bool Client::connectTo(QString adress, quint16 port) { QHostAddress adressToCo; adressToCo.setAddress(adress); QObject::connect(m_socket, &QTcpSocket::readyRead, [=] { QString msg = m_socket->readAll(); emit msgRcvd(msg); }); m_socket->connectToHost(adressToCo, port); if (!m_socket->waitForConnected(5000)) { m_errorMsg = m_socket->errorString(); qDebug() << "Error socket:" << m_socket->errorString(); return false; } else if(m_socket->state() != QAbstractSocket::ConnectedState) { m_errorMsg = m_socket->errorString(); qDebug() << "Error socket:" << m_socket->errorString(); return false; } qDebug() << "Connect corectly to " + adress; m_socket->write("I'm writing a message that will be lost somewhere over the rainbow"); return true; }
I gave the same adress port to both functions :
Adress: "192.168.1.42" Port: 58513Regardless the port number I put in,
I also could set the adress to "127.0.0.1" it is always the same issue:
the client is connected.Output of software:
Adress: "192.168.1.42" Port: 58513 "Bind corectly to 192.168.1.42" "Connect corectly to 192.168.1.42" Client connected: true
to get the final line i'm using :
m_socket->state() == QAbstractSocket::ConnectedState ? true : false;
So everytimes the QTcpSocket connect itself to my adress regardless of my port...
When I launch my server, the server doesn't have any connection either -
I have the same issue no listening server at the adress port but it's connecting anyway:
bool Client::bindTo(QString adress, quint16 port) { QHostAddress adressToCo; if (adressToCo.setAddress(adress) == true) { m_socket = new QTcpSocket; if (!m_socket->bind(adressToCo, port)) { m_errorMsg = "Cannot connect to " + adress; qDebug() << "Cannot connect to " + adress; } } else { m_errorMsg = "Wrong IP address."; qDebug() << "Wrong IP address: " + adress; return false; } qDebug() << "Bind corectly to " + adress; return true; } bool Client::connectTo(QString adress, quint16 port) { QHostAddress adressToCo; adressToCo.setAddress(adress); QObject::connect(m_socket, &QTcpSocket::readyRead, [=] { QString msg = m_socket->readAll(); emit msgRcvd(msg); }); m_socket->connectToHost(adressToCo, port); if (!m_socket->waitForConnected(5000)) { m_errorMsg = m_socket->errorString(); qDebug() << "Error socket:" << m_socket->errorString(); return false; } else if(m_socket->state() != QAbstractSocket::ConnectedState) { m_errorMsg = m_socket->errorString(); qDebug() << "Error socket:" << m_socket->errorString(); return false; } qDebug() << "Connect corectly to " + adress; m_socket->write("I'm writing a message that will be lost somewhere over the rainbow"); return true; }
I gave the same adress port to both functions :
Adress: "192.168.1.42" Port: 58513Regardless the port number I put in,
I also could set the adress to "127.0.0.1" it is always the same issue:
the client is connected.Output of software:
Adress: "192.168.1.42" Port: 58513 "Bind corectly to 192.168.1.42" "Connect corectly to 192.168.1.42" Client connected: true
to get the final line i'm using :
m_socket->state() == QAbstractSocket::ConnectedState ? true : false;
So everytimes the QTcpSocket connect itself to my adress regardless of my port...
When I launch my server, the server doesn't have any connection either@Darta said in QTcpSocket connecting even tho no server?:
I have the same issue no listening server at the adress port but it's connecting anyway:
"Bind corectly to 192.168.1.42"
"Connect corectly to 192.168.1.42"
I don't understand. Your
bindTo()
doesthe listening, theconnectTo()
connects to that, they are both using the same port number, your program is acting as both client and server. That's how it looks to me, so what's the problem? -
@Darta said in QTcpSocket connecting even tho no server?:
I have the same issue no listening server at the adress port but it's connecting anyway:
"Bind corectly to 192.168.1.42"
"Connect corectly to 192.168.1.42"
I don't understand. Your
bindTo()
doesthe listening, theconnectTo()
connects to that, they are both using the same port number, your program is acting as both client and server. That's how it looks to me, so what's the problem?@JonB I divid it in two functions because my server only use one, but my client is using both like that :
QSettings vmIni("qrc:/VirtualHome.ini"); QString ip = vmIni.value("ServerIP").toString(); quint16 port = vmIni.value("ServerPort").toUInt(); qDebug() << "Adress:" << ip << "Port:" << port; if (m_client.bindTo(ip, port)) { if (!m_client.connectTo(ip, port)) qDebug() << "Fail to connect to server."; } else qDebug() << "Fail to bind to server"; qDebug() << "Client connected:" << m_client.isConnected();
-
@JonB I divid it in two functions because my server only use one, but my client is using both like that :
QSettings vmIni("qrc:/VirtualHome.ini"); QString ip = vmIni.value("ServerIP").toString(); quint16 port = vmIni.value("ServerPort").toUInt(); qDebug() << "Adress:" << ip << "Port:" << port; if (m_client.bindTo(ip, port)) { if (!m_client.connectTo(ip, port)) qDebug() << "Fail to connect to server."; } else qDebug() << "Fail to bind to server"; qDebug() << "Client connected:" << m_client.isConnected();
@Darta
I suggest you take this to your own thread which you start. It won't be to do with this old one.I devid it in two function because my server only use one of those but my client where i'm using both directly at the same moment like that :
I'm afraid I don't understand this sentence and doubt others will.
QTcpSocket
is not going to be successfully connecting to an IP/port if there is nothing bound to and listening on that. -
@Darta
I suggest you take this to your own thread which you start. It won't be to do with this old one.I devid it in two function because my server only use one of those but my client where i'm using both directly at the same moment like that :
I'm afraid I don't understand this sentence and doubt others will.
QTcpSocket
is not going to be successfully connecting to an IP/port if there is nothing bound to and listening on that. -
@Darta
I suggest you take this to your own thread which you start. It won't be to do with this old one.I devid it in two function because my server only use one of those but my client where i'm using both directly at the same moment like that :
I'm afraid I don't understand this sentence and doubt others will.
QTcpSocket
is not going to be successfully connecting to an IP/port if there is nothing bound to and listening on that.@JonB said in QTcpSocket connecting even tho no server?:
QTcpSocket
is not going to be successfully connecting to an IP/port if there is nothing bound to and listening on that.why can't you just trust what I've experienced ?
I'm telling you that I have no server and using both functions like I shown previously I get the ouptut connected every times it's not to debate I'm asking if I've done something wrong that would make it that way nothing more but nothing less -
@JonB said in QTcpSocket connecting even tho no server?:
QTcpSocket
is not going to be successfully connecting to an IP/port if there is nothing bound to and listening on that.why can't you just trust what I've experienced ?
I'm telling you that I have no server and using both functions like I shown previously I get the ouptut connected every times it's not to debate I'm asking if I've done something wrong that would make it that way nothing more but nothing lessPlease provide a minimal, compilable example, not some code fragments you're insisting that nothing else is running and we can't check.