Results 1 to 2 of 2

Thread: Unable to read from QTcpSocket

  1. #1
    Join Date
    Mar 2007
    Posts
    8
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Unable to read from QTcpSocket

    I'm trying to read a TCP stream sent from a third party TCP server. I've create a thread that is able to connect to the server succesfully but is unable to read any of the data being transmitted. I've tried to determine if I've got data using the readyRead signal and by periodically checking the bytesAvailable() method.

    I have disable my systems firewall programs. I have also been able to receive the data from the third party server using the TCPBuilder TCP testing utility and using windows hyperterminal.

    Here is a code snippet. Any help would be greatly appreciated.

    void TCPClientServer::run()
    {
    this->m_bolThreadIsRunning = true;

    this->m_bolRunning = this->initClientTcp();

    while(this->m_bolRunning)
    {
    if(!this->m_ptrTcpSocket->isOpen())
    {
    this->m_ptrTcpSocket->connectToHost(this->m_strIpAddress,
    this->m_intPortNumber, QIODevice::ReadWrite);
    if(!this->m_ptrTcpSocket->waitForConnected(3000))
    {
    this->m_ptrTcpSocket->close();
    }
    }
    else if(this->m_ptrTcpSocket->bytesAvailable() > 0)
    {
    emit this->sendSocketState("Valid Socket");
    }

    // Go to sleep for 1 second
    this->msleep(500);
    }
    this->m_ptrTcpSocket->close();
    if(this->m_ptrTcpSocket != (QTcpSocket*)NULL)
    delete this->m_ptrTcpSocket;

    if(this->m_ptrDataStream != (QDataStream*)NULL)
    delete this->m_ptrDataStream;

    this->m_bolThreadIsRunning = false;
    }

    bool TCPClientServer::initClientTcp()
    {
    // Create the socket
    this->m_ptrTcpSocket = new QTcpSocket;

    // Setup the signals and slots
    connect(m_ptrTcpSocket, SIGNAL(error(QAbstractSocket::SocketError)), this,
    SLOT(socketError(QAbstractSocket::SocketError)), Qt:directConnection);
    connect(m_ptrTcpSocket, SIGNAL(stateChanged(QAbstractSocket::SocketState)) ,
    this, SLOT(slotSocketState(QAbstractSocket::SocketState) ),Qt:directConnection);
    connect(m_ptrTcpSocket, SIGNAL(readyRead()), this, SLOT(slotReady()),
    Qt:directConnection);

    this->m_ptrDataStream = new QDataStream(this->m_ptrTcpSocket);

    return(true);
    }

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Unable to read from QTcpSocket

    If you want to use the asynchronous QTcpSocket interface, get rid of that while loop and msleep() and invoke QThread::exec() to start the event loop. Otherwise you have to invoke waitForReadyRead() to allow the socket to receive data.

  3. The following user says thank you to jacek for this useful post:

    jimroos (5th July 2007)

Similar Threads

  1. How to read more bytes using QTcpSocket?
    By vishesh in forum Qt Programming
    Replies: 1
    Last Post: 3rd July 2007, 20:23
  2. How to read CD with read?
    By vishal.chauhan in forum Qt Programming
    Replies: 6
    Last Post: 29th June 2007, 08:20
  3. Replies: 2
    Last Post: 23rd June 2007, 09:13
  4. Strange QTcpSocket behavior (debugged with Ethereal)
    By mdecandia in forum Qt Programming
    Replies: 23
    Last Post: 28th May 2007, 20:44
  5. QIODevice read()
    By ShaChris23 in forum Newbie
    Replies: 1
    Last Post: 3rd May 2007, 00:29

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.