QTcpSocket never triggering readyRead()
I have tried following the tcpSocket fortune example in order to implement my own tcpSocket needs, but I am still having trouble in that my readyRead() is never true. I initialize the socket with my object in data.cpp:
data::data(QObject *parent) :
socket = new QTcpSocket(this);
connect(socket, SIGNAL(readyRead()), this, SLOT(getData()));
QByteArray hello = "PASSWORD\t\"hi\"\x00"; socket->write(hello);
But I know that my data retrieval function is never triggered because I never get to my debug statement:
qDebug() << "here \n\n\n\n\n";
in >> data;
When I run the program in main.cpp, I simply get the UI coming up, but no messages concerning the socket:
int main(int argc, char *argv)
QApplication a(argc, argv);
data *myData = new data();
Does anyone have an idea as to what is going on, and why readyRead() never returns true?
There is insufficient information to tell us why this is failing. Here are some observations and questions to ask yourself.
You don't need to explicitly call open() on the QTcpSocket, that will be done by the connectToHost() call.
The flush() on line 7 has no purpose.
Is the specified host listening on that interface and port? Is an error reported?
Does your write() call return the expected number of bytes?
Are warning message visible in the program console/debug output?
Have you verified that the server receives the bytes?
Have you verified that the server sends a response?
Even if you received a readyRead() signal you have not heeded the warning in the docs regarding checking that there are sufficient bytes available before using the streaming operator. The entire response may not arrive in a single chunk. This code may still not function.