QTcpSocket never triggering readyRead()
-
Hi,
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) :
QObject(parent), networkSession(0)
{
socket = new QTcpSocket(this);
socket->open(QTcpSocket::ReadWrite);
connect(socket, SIGNAL(readyRead()), this, SLOT(getData()));
socket->flush();
connect(socket, SIGNAL(error(QAbstractSocket::SocketError)),
this, SLOT(displayConnectionError(QAbstractSocket::SocketError)));
socket->connectToHost("172.30.0.5", 6543);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:
@
void data::getData(){
qDebug() << "here \n\n\n\n\n";
QDataStream in(socket);
in.setVersion(QDataStream::Qt_4_0);
QString data;
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);
QApplication::setGraphicsSystem("raster");
MainWindow w;
w.setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
w.setViewport(new QGLWidget);
w.setResizeMode(QDeclarativeView::SizeViewToRootObject);
data *myData = new data();
w.rootContext()->setContextProperty("appData", myData);
w.show();
return a.exec();
}@
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.