UDP broadcast



  • Hi, I'm finding 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 
    

    their 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. The errorString() method returns "unable to send a message". 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.



  • Hello and welcome!

    Is the actual Broadcast Sender example working for you?
    It would be helpful if you could show us your (relevant) code.



  • Thank for you reply. the actual example doesn't work for me.
    Here is my code:

     envoyeurUDP=new QUdpSocket(this);
        recepteurUDP=new QUdpSocket(this);
        //démarrage du serveur pour UDP
        if (!recepteurUDP->bind(QHostAddress::AnyIPv4,8787,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)


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    How do you know it doesn't work ?



  • writedatagram() returns -1 ,error() returns SocketError::NetworkError


  • Lifetime Qt Champion

    I've missed a point in one of your answer. Did I understand correctly that the "Broadcast Sender Example" failed for you ?

    If so, then before trying to debug your application, you should check what makes the example fail. The code doesn't mix both sending and receiving.



  • What's size of data? If it's > 512 bytes it will fail.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.