4
votes

I'm searching since two days how to sent a UDP broadcast. I have seen many examples (also the broadcastReceiver and broadcastSender on the qt web site) but they are still not working for me. When I try to send a broadcast, the method QUdpSocket::writeDatagram(..) returns -1 and the datas are not transmitted. I'm on ubuntu 14.04 x64. I wish to know if there is a particular manipulation that I should do to make this broadcast work. Thanks and sorry for my english level.

that is my code

 envoyeurUDP=new QUdpSocket(this);
recepteurUDP=new QUdpSocket(this);
//démarrage du serveur pour UDP
if (!recepteurUDP->bind(QHostAddress::AnyIPv4,7878,QUdpSocket::ShareAddress | QUdpSocket::ReuseAddressHint)) {
  qDebug("Impossible de créer la socket en écoute");
 exit(EXIT_FAILURE);
}

connect(recepteurUDP, SIGNAL(readyRead()), this, SLOT(lireDatagrams()));
void FileTransferManager::lireDatagrams(){

char* donnees;
while (recepteurUDP->hasPendingDatagrams()) {
        QByteArray datagram;
        datagram.resize(recepteurUDP->pendingDatagramSize());
        recepteurUDP->readDatagram(datagram.data(), datagram.size());
        donnees=datagram.data();
    }}

and this one to send datagrams

void FileTransferManager::sendDatagram(string msg, QHostAddress addr){
QByteArray datagram=msg.data();
    qint64 r=envoyeurUDP->writeDatagram(datagram.data(), datagram.size(),  addr, 7878);}

and i use it like this :

fileManager.sendDatagram("blabla",QHostAddress::Broadcast);

here is the output of my ifconfig command:

eth0 Link encap:Ethernet HWaddr f8:a9:63:2d:89:ff
UP BROADCAST MULTICAST MTU:1500 Metric:1 Packets reçus:0 erreurs:0 :0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 lg file transmission:1000 Octets reçus:0 (0.0 B) Octets transmis:0 (0.0 B)

lo Link encap:Boucle locale
inet adr:127.0.0.1 Masque:255.0.0.0 adr inet6: ::1/128 Scope:Hôte UP LOOPBACK RUNNING MTU:65536 Metric:1 Packets reçus:5311 erreurs:0 :0 overruns:0 frame:0 TX packets:5311 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 lg file transmission:0 Octets reçus:661728 (661.7 KB) Octets transmis:661728 (661.7 KB)

wlan0 Link encap:Ethernet HWaddr b8:ee:65:ab:4f:77
inet adr:10.42.0.1 Bcast:10.42.0.255 Masque:255.255.255.0 adr inet6: fe80::baee:65ff:feab:4f77/64 Scope:Lien UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 Packets reçus:14069 erreurs:0 :0 overruns:0 frame:0 TX packets:15529 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 lg file transmission:1000 Octets reçus:6007592 (6.0 MB) Octets transmis:2307422 (2.3 MB)

1
Need a short example of code demonstrating the problem. Otherwise you probably aren't going to get any help here. Output from your ifconfig would be helpful too, to check that the address is indeed broadcast.Sergei Tachenov
thank you I have edited my post in order to add the codeKlaus NGUETSA
Interesting. What do error() and errorString() on the datagram socket return after the call to writeDatagram() fails?Sergei Tachenov
errorString() returns "unable to send a message"Klaus NGUETSA
error() returns a SocketError object and i don't know how to print it. How can i print it?Klaus NGUETSA

1 Answers

0
votes

I know this is old, but this works for me on Ubuntu 16.04 LTS to broadcast messages to any listener:

QUdpSocket *udp = new UdpSocket(this);
QByteArray dgram = "hello";
udp->writeDatagram(dgram.data(), dgram.size(), QHostAddress::Broadcast, ipPort);