Qt World Summit: Register Today!

QTcpSocket not sending bytes

  • Hi, I have a function in a qtcpserver that sends a small message to a buncha sockets but for some reasons the bytes are not sent threw the network until another package from a different function is sent.. has anyone ever witnessed that strange behavior?! I tried using the flush() method to force the transfer but nothing seems to be working..

  • Can you show the code where you send the packet? I've never experienced this, especially if you are flushing.

    When you flush you're telling it to send over the network right away and not wait for buffering.

    Assuming all the code is good you could have some network routing issues. A router or switch on your network holding the packet until it gets more data, essentially buffering it. Some advanced routers do this. On a personal network at home this wouldn't be the issue unless you spent some serious dollars on a router or switch.

    You can use something like wireshark to watch the actual network traffic too. Maybe it's the socket on the other end that isn't processing the data right away or waiting for more before it handles it.

    There are too many variables to give much more of an answer than this. Some code would help out quite a bit.

  • @void Server::sendMessage(const QString & p_pseudo, const QString & name)
    //qDebug() << "Valid socket: " << socket->isValid();

    QByteArray paquet;
    QDataStream out(&paquet, QIODevice::WriteOnly);
    out << (quint16) 0;
    Message message();
    out << message;
    out << (quint16) (paquet.size() - sizeof(quint16));


  • I'm sur it's not routing issues cause as I said when another network event happens my package is processed... plus my server and my apps and are on the same machine.. tho I'm not using the loopback interface... the only thing that is different about this function from the others is that I call it from a different class.. usually I have my slot incomingData() in the server class that I then call send function from.. but this particular function I call it from another class

  • Well so far I see a problem with line 10. I'm surprised that even lets you compile. For instance you could use Message msg = Message() but doing Message message() isn't going to give you the results you expect. I don't think that is your problem though. "explanation of instantiation.":http://stackoverflow.com/questions/2308646/different-ways-of-constructing-an-object-in-c

    Anyway, the only other thing that I can see from the code that might be off is in your use of the socket map. If you reference the socket map with [] operator you may not get the value you expect. Since using [] will add the value if it doesn't exist that can cause problems. To be safe I would verify that code is getting the correct item before using or at least do:


    I guess on the map value being returned as an int that could be used with socketlist.at().

    It feels like your server is having a problem picking up the stuff from the socket. Not at a socket level but at a code level. Something in there is making it think that it didn't receive the response when in fact it did. This is just a guess from my past experience. Very hard to debug something like this without the code and the big picture to understand it.

    You could try taking your code out of it as much as possible. Write a very simple socket server that just echos what is sent over the socket. See if it is getting the information.

    Again you could try a packet sniffer like wireshark to find out if the data is actually sent and when. It may not be being sent until later on in your code or, what I'm thinking, is that it is actually sent right away when you expect it to be sent but the server isn't processing it properly.

  • Qt Champions 2017

    Can you try sending small data using QTextStream instead of QDataStream ?

    Just try something like follows. This reads the data from console and dumps the data to socket.

    @void ConsoleClient::sendData(){
    QTextStream stream(stdin);
    qDebug() << "Connected to Server - Sending Data";
    QString line;
    do {
    line = stream.readLine();
    qDebug() << "Sending the line =" << line;
    } while(!line.isNull());

Log in to reply