Unsolved QTcpSocket connecting even tho no server?
-
@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.
-
@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. -
@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 -
@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();
-
@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 my server is irrelevant since i'm not launching it
-
@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 -
Please provide a minimal, compilable example, not some code fragments you're insisting that nothing else is running and we can't check.
-
@Christian-Ehrlicher as previously suggested I created my own topic here