Important: Please read the Qt Code of Conduct -

QtcpServer lost message

  • I am deveropping TCPIP communication program.
    My project send messege from one client to server.
    But my project fail to receive same message.when It send with a short span.
    If it send with a log span.It can receive all message.
    Please tell me how to solve this probrem.

    To deverop it, I refer to fortune client server example.
    My projects(both client and server) have MainWindow.
    so I use it to connect QtcpSocket and Qtcpserver. is it bad?
    Please tell me how to solve it.

  • welcome to devnet

    The fortune example worked for you?
    What are the main differences you have applied to fortune example code?
    What is a short message for you?

  • The fortune example worked for you?
    Yes it work
    What are the main differences you have applied to fortune example code?>
    fortune example send message from server to client.
    but my project send mesaage from client to server
    and my send message is I send it like this
    out << (quint16)0;
    out << (quint16)(block.size() - sizeof(quint16));
    and read it by readbytes
    What is a short message for you?
    "short "is not meaning message length "short" is time span
    so when i send it quickly(by short time spen) .some message fail to reach

    [Edit] Code wrappings introduced, koahnig

  • Could it be that you are sending two or more messages, but it will be read as "one"?
    e.g. if you send two messages directly in sequence, they may be already at your server when you start to the first one. The consequence could be that you are reading both at one without noticing.

  • Thank you.

    but it will be read as “one”
    As i know only it.
    I understand sometime "readyread" slot function have two or more sended message.
    Please tell me to how to know rest messages are exist or not . bitesAvailable()?

  • It is a bit hard to provide a sound advice. Your example is a bit obscure for me.
    Typically you would have a type of header for a message. This header may have additional information or just the length of information you are sending.
    I assume that the first byte you are sending in the first line has the function as header. Then you seem to dump a number of bytes. However, how does the counterpart know the number of bytes sent?

    You could use something like this:
    bool TcpSocket :: write (const std :: string & str)
    int len = int (str.length());
    if (! write ((char *) & len, sizeof (len)))
    return false;

    if (len > 0)
        if (! write ((char *) & str [0], len))
            return false;
    return true;

    This writes the number of bytes (here the length of the string). Afterwards the string is sent byte by byte. On the counterpart you would receive the length of teh string and know exactly the number of bytes to follow.
    My guess is that this is currently your problem. You receive an event and read all bytes, but in fact you are receiving two messages without recognizing that.

Log in to reply