Crash on wiritting data to a socket

  • How I can check if my socket contains data a.k.a it is not empty?

       QBuffer buf(&data);;,"PNG");
       socket->isEmpty() ?? --- does this even exist?

  • @mandruk1331 WHat you're looking for is the QIODevice::bytesWritten Signal that is emitted once the Device has written its bytes :-)

  • @J.Hilk I'll try that one. And how I can clear the data from a socket?

  • Moderators

    @mandruk1331 What do you mean by clear? If the data was written it is not anymore in your socket.

  • @mandruk1331 said in Check if socket contains data:

    @J.Hilk I'll try that one. And how I can clear the data from a socket?

    For writing, that should happen automatically as soon as the data is written.
    For reading, that should also happen automatically as soon as your read the data QIODevice::Read, QIODevice::ReadAll, QIODevice::ReadLineetc

  • @jsulm
    I thought that I write data into the socket the data is being read from the server side and then I have to clear the socket or the data is being written to the socket and then sent to server side instantly?
    And in the code below to what exactly I have to connect the signal bytesWritten to the buf?
    smth like this:

      QBuffer buf(&data);;,"PNG");

  • Moderators

    @mandruk1331 No, you send the data, when it was sent then there is nothing in the buffer any-more (why should be still there?).

    connect(socket, SIGNAL(bytesWritten(qint64)), WHOEVER, SLOT(onBytesWritten(qint64)));

    WHOEVER - you should know to whom to connect. It depends on what you want to do and on your design.

  • @jsulm Ok got it, thank you!
    And my last qyestion:

    QBuffer buf(&data);;,"PNG");
      qDebug()<<"Bytes written:"<<socket->writeDatagram(buf.buffer(),QHostAddress::LocalHost,PORT);

    data - QByteArray
    frame - QImage
    socket - QUDPSocket
    at the line I get a runtime error, what can b e the problem?
    My goal is to write a QImage to a socket.

  • Moderators

    @mandruk1331 What exact error do you get?
    Did you check the return value when calling See

  • @jsulm Yes the result is true (it is opened). When I call it just crashes.
    QBuffer buf(&data);
    QByteArray g =;
    This one also throws an error. I think that buf just has no data but the variable data has the data

  • Moderators

    @mandruk1331 You should start your app through the debugger in QtCreator - then you will see the stack trace and what the issue actually is.

  • @jsulm I checked the 'ba' variable has data in it, the img is being saved at the moment the crashes when it tries to write 'ba' to the socket.

    void SocketUDP::SendFrame(QImage frame){
        QByteArray ba;
        QBuffer buffer(&ba);;
        if(, "JPEG")){
            qDebug()<<"Image saved";

  • If someone encounters the same problem, then here is the solution:
    I was calling this function in a thread and inside that thread I called the new operator a couple of times, which caused the segmentation fault when trying to write to UDP socket, after I remove most of the new operator the problem has been solved.

Log in to reply