0
votes

I use QUDPsocket to send message. And I connect: connect(socket,SIGNAL(ReadyRead()),this,SLOT(processPendingDatagrams()));

when I call the sendmessage("123") in the main,the processPendingDatagrams() only run once.

but when I call the sendmessage("123") in the button click event, this runs twice . how to solve the problem?

sendmessage():

socket->writeDatagram(data,data.length(),QHostAddress::Broadcast, port);

1
are you sending and receiving in the same application?Mike
no, I receive in socket classYOUNG

1 Answers

0
votes

If the server notices it once or twice depending on what the client does, then it's the client's fault.

In your click event, add something like:

qDebug() << "Click event, sending message...";

You'll probably notice it's sent twice, on click down and when the button is released. Instead you can do something like:

connect(myButton, &QPushButton::clicked, [this]() {sendMessage("123");}

Or do your own slot.