Solved 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); buf.open(QIODevice::WriteOnly); frame.save(&buf,"PNG"); socket->writeDatagram(buf.buffer(),QHostAddress::LocalHost,PORT); 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?
-
@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 dataQIODevice::Read
,QIODevice::ReadAll
,QIODevice::ReadLine
etc -
@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:
connect(this,SIGNAL(bytesWritten),this,SLOT(DataHasBeenWritten())):?QBuffer buf(&data); buf.open(QIODevice::WriteOnly); frame.save(&buf,"PNG"); socket->writeDatagram(buf.buffer(),QHostAddress::LocalHost,PORT);
-
@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); buf.open(QIODevice::WriteOnly); frame.save(&buf,"PNG"); qDebug()<<"Bytes written:"<<socket->writeDatagram(buf.buffer(),QHostAddress::LocalHost,PORT);
data - QByteArray
frame - QImage
socket - QUDPSocket
at the frame.save line I get a runtime error, what can b e the problem?
My goal is to write a QImage to a socket. -
@mandruk1331 What exact error do you get?
Did you check the return value when calling buf.open()? See http://doc.qt.io/qt-5/qiodevice.html#open -
@jsulm Yes the result is true (it is opened). When I call frame.save it just crashes.
QBuffer buf(&data);
QByteArray g = buf.data();
This one also throws an error. I think that buf just has no data but the variable data has the data -
@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); buffer.open(QIODevice::WriteOnly); if(frame.save(&buffer, "JPEG")){ qDebug()<<"Image saved"; } qDebug()<<buffer.buffer(); socket->writeDatagram(ba,QHostAddress::LocalHost,PORT); }
-
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.