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);
    connect(socket, SIGNAL(readyRead()), this, SLOT(getData()));
    connect(socket, SIGNAL(error(QAbstractSocket::SocketError)),
    this, SLOT(displayConnectionError(QAbstractSocket::SocketError)));
    socket->connectToHost("", 6543);

    QByteArray hello = "PASSWORD\t\"hi\"\x00";


    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);
    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);
    MainWindow w;
    w.setViewport(new QGLWidget);
    data *myData = new data();
    w.rootContext()->setContextProperty("appData", myData);
    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.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.