Thread: can't read from qt4 tcpSocket

  1. #1
    Registered User
    Join Date
    Dec 2005
    Location
    german border
    Posts
    72

    can't read from qt4 tcpSocket

    Hi,

    I am using Qt4 to develop a simple chat client for school. I have already asked this question on qtforums.org but never got a response, I am hoping that someone here has some knowledge of Qt and can shed some light on this problem for me. I have basically taught myself OO programming for this project, so I realise that my design is probably not the best, but this bug is baffling me. Here is the relevant code

    Code:
    void Kether::readData()
    {
        //qDebug("In readData start");
        QDataStream in(tcpSocket);
        in.setVersion(QDataStream::Qt_4_2);
        /*
        if (blocksize == 0)
        {
            if (tcpSocket->bytesAvailable() < (int)sizeof(quint16))
            {
                qDebug("in first return");
                return;
            }
            in >> blocksize;
        }
    
        if (tcpSocket->bytesAvailable() < blocksize)
        {
            qDebug("in second return");
            return;
        }
        */
        QString receivedMessage;
        in >> receivedMessage;
        qDebug() << "receivedMessage:" << receivedMessage;
    
        ui.chatWindow->append(receivedMessage);
        qDebug("In readData end");
    }
    This slot/function is executed everytime the readyRead() signal is emitted which means that data is available to read from the network socket. The function is called, but receivedMessage is always blank so it isn't reading from the socket. But there is no reason it shouldn't, it's definetely connected and will send data to netcat and this function is called to read the data and display except it only reads a blank string. I would appreciate any replies or suggestions, I will post all code on request, it's just that's it's a few hundred lines so I am a little hesitant.

    Thanks in advance,

    Calef13

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    >> is for the formatted input
    socket should provide you with binary data. Shouldn't you use some binary input functions?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Dec 2005
    Location
    german border
    Posts
    72
    Well, the examples simply read into a qstring and they work fine, but you are right the datastream is for binary writing, I want to try this but what type of variable should I use, will an arry work okay? I used those when I used the c sockets library.

  4. #4
    Registered User
    Join Date
    Dec 2005
    Location
    german border
    Posts
    72
    ok I just tried it with QTextStream and it worked! Thank you so much, you have no idea how long I have been fighting with this, seriously thanks a lot.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Read() problems
    By yay4rei in forum C Programming
    Replies: 2
    Last Post: 07-21-2005, 10:47 AM
  2. How can I know the actual bytes read in a file read
    By pliang in forum C++ Programming
    Replies: 1
    Last Post: 06-08-2005, 04:23 PM
  3. What Would You Use To Read User Input?
    By djwicks in forum C Programming
    Replies: 11
    Last Post: 04-05-2005, 03:32 PM
  4. Read Array pro!!Plz help!!
    By Supra in forum C Programming
    Replies: 2
    Last Post: 03-04-2002, 03:49 PM
  5. Help! Can't read decimal number
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 09-07-2001, 02:09 AM