QTcpSocket connecting even tho no server?
-
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.
-
Please provide a minimal, compilable example, not some code fragments you're insisting that nothing else is running and we can't check.