Problems with Open-Source Downloads read https://www.qt.io/blog/problem-with-open-source-downloads and https://forum.qt.io/post/638946
[Solved] Understanding QUdpSocket - deleting
I have some problems understanding how QUdpSocket has to be used.
My target is to send to a single IP and Port and also receive from the same IP and Port (log server with ack).
This returns a bind error:
@if((lola_socket->bind(QHostAddress(ip4addr), port.toUInt(&ok, 10))) != true)
qDebug() << "bind error for LoLa";
and this works:
@ if((lola_socket->bind(QHostAddress::Any, port.toUInt(&ok, 10))) != true)
qDebug() << "bind error for LoLa";
For sending: Do I have to give the target address as argument on every send? How can I preset this on the socket?
For receiving: How do I set the single IP to receive from it (only), or is it usual to listen from any IP on a given port with Any?
Xander84 last edited by
first do you have to use UDP for this? To me it sounds like TCP would be better suited for your use case?
UDP is connection-less, so you don't have to bind to the socket to any IP and port at all, the QUdpSocket class gives you this option only for convenience, but calling "bind" does not actually open a connection and bind to the socket if you use UDP. Same for receiving data, I don't think you can onyl receive data from one IP, the UDP socket receives everything it gets, you can of course ignore every datagram you don't want other than from the one IP.
Hope that helps you.
Thanks for your inputs and clarification. The server I have to communicate with is nothing that I can influence and it waits for log messages on a specified UDP port and acknowledges the messages to the source on the same port.
I'll set it up to just listen on the port. If I need to filter the source IP I can still do this with the information returned from QUdpSocket::readDatagram.
It works :-)
There is one thing though: How do I reconfigure, close or terminate a socket bound? If I call it again with a different port for instance, it creates a new socket (netstat -a). Tried closing .. leads to SEGV.
Need to kick this up again ... anybody an idea how to close/terminate or delete socket if it's not used anymore?
Jena43 last edited by
"QUdpSocket destructor":http://qt-project.org/doc/qt-5/qudpsocket.html#dtor.QUdpSocket automaticaly closes the connection.
bq. QUdpSocket::~QUdpSocket() [virtual]
Destroys the socket, closing the connection if necessary.
Have you tried to @delete lola_socket;@
What Jena43 said will do it, and you can specifically do it without destroying your object with:
Yup, works this time :)
I already tried delete - to no avail (SIGSEGV) - some time ago. Since you came up with the same suggestion again, I tried again and to my surprise: it works!
I dont know what I did wrong last time ...
Both seem to work this time.
Since my code currently creates a new one if the old one had to be closed:
@lola_socket = new QUdpSocket(this);@
I think I am gone use
As a side note sometimes when I have a crash using delete directly on Qt objects it has to do with outstanding signals that haven't been handled by that object yet.
In those situations you can always do
and the next time the exec loop comes around it will clean up that object once everything it had outstanding has been processed by the event loop.
Which is why it works now but didn't before. Different things in the code led to different outstanding signals which you didn't have this time around.
Oh, and once you call deleteLater() assume that object and it's pointers are dead and don't use them again, even though they may remain valid for a little bit. After a deleteLater() I tend to set my pointers to 0 just to be sure that if I accidentally use them it's easy to catch.
What happens if I create a new lola_socket before the old one has been deleted?
Once you call deleteLater() you can act as if it had been deleted. You can create a new one, even over the same pointer.
Even if it was still connected by the time your new socket went to connect the event loop would have cleaned up the old one.
So basically, you can think it is deleted just as if you had done delete ptr; on it.
Thanks a lot for the explanation.