QTcpSocket Client QT 5.12 not connecting
-
Hey, some background info: I use the QT extension in visual studio 2022, QT version 5.12.2. I want to send a simple "J" character to a server from my client using TCP. I got this to work using a different TCP socket code that is not part of the QT framework. So the communication and setup outside the code works but now I need it also to work using QT's framework, QTcpSocket.
I have tested different versions that I found on the internet like the fortune client example, so far I have not gotten any of them to work. I am not too experienced with TCP coding so to keep it short, the simplest way to connect to server at ip=192.168.3.154 and port=4035 and send "J" as I understand could look like this:
QTcpSocket *socket = new QTcpSocket(); socket->connectToHost("192.168.3.154", 4035); // we need to wait... if (!socket->waitForConnected(5000)) { qDebug() << "Error: " << socket->error() << "State: " << socket->state(); } socket->write("J");But in the code above I get a timeout so it seems I cannot make the connection this way. Am I misusing the QTcpSocket object?
Edit: Added some more error handling. The message from the qDebug() is: Error: QAbstractSocket::SocketTimeoutError State: QAbstractSocket::UnconnectedState
-
I found the issue, of course I had a digit wrong on the port... The connection seems to succeed now, I get a different error elsewhere but for the purpose of this thread it can be considered solved.
-
Hey, some background info: I use the QT extension in visual studio 2022, QT version 5.12.2. I want to send a simple "J" character to a server from my client using TCP. I got this to work using a different TCP socket code that is not part of the QT framework. So the communication and setup outside the code works but now I need it also to work using QT's framework, QTcpSocket.
I have tested different versions that I found on the internet like the fortune client example, so far I have not gotten any of them to work. I am not too experienced with TCP coding so to keep it short, the simplest way to connect to server at ip=192.168.3.154 and port=4035 and send "J" as I understand could look like this:
QTcpSocket *socket = new QTcpSocket(); socket->connectToHost("192.168.3.154", 4035); // we need to wait... if (!socket->waitForConnected(5000)) { qDebug() << "Error: " << socket->error() << "State: " << socket->state(); } socket->write("J");But in the code above I get a timeout so it seems I cannot make the connection this way. Am I misusing the QTcpSocket object?
Edit: Added some more error handling. The message from the qDebug() is: Error: QAbstractSocket::SocketTimeoutError State: QAbstractSocket::UnconnectedState
@Daddedebad Please add error handling to your code: https://doc.qt.io/qt-6/qabstractsocket.html#error
-
@Daddedebad Please add error handling to your code: https://doc.qt.io/qt-6/qabstractsocket.html#error
@jsulm I updated the code now and edited in the return from the error.
-
I found the issue, of course I had a digit wrong on the port... The connection seems to succeed now, I get a different error elsewhere but for the purpose of this thread it can be considered solved.