Multicast/Broadcast questions

  • Hello.

    I'm trying to develop some client/server software.

    I've found some problems that can be reproduced ussing broadcast/multicast examples: Running both in the same computer, works. But, if server is in a Windows 7 machine with more than one network interface, broadcast/multicast paquets don't reach client software. It's a known issue:
    It's talking about broadcast, but it's the same with multicast. For sure, Unix-like machines handles correctly broadcast writing datagrams (writes it in all interfaces), but I'm not sure about multicast in this environments.

    I'm tying to work around, but I'm complety confused. I though the solution were chossing the network interface to write datagrams to. But I found no function to that. There is setMulticastInterface, but it's just for reading (must be bounded).

    Someone can help me with it?


  • Hi,

    what means " But, if server is in a Windows 7 machine with more than one network interface, broadcast/multicast paquets don’t reach client software"??

    Please specify your Client and Server LAN configurations (IP for each network interface) and post your code.

    The Operating system choose the network interface to use to send packets depending to destination IP and interface IP/Netmask

  • Sorry, better change server/client by sender/receiver. I think it will be more understandable

    Don't need to post code; there are two pairs of examples in QtCreator:
    "multicast receiver": / "multicast sender": and "broadcast receiver": / "broadcast sender":

    But, the relevant code is:
    Broadcast Sender:
    @udpSocket->writeDatagram(, datagram.size(), QHostAddress::Broadcast, 45454);@
    Broadcast Receiver:
    @udpSocket->bind(45454, QUdpSocket::ShareAddress);@
    broadcast address allways is (
    Operating systen must send it to ALL interfaces, but, windows 7 sends it just to the first interface it founds.

    Multicast Sender:
    @udpSocket->writeDatagram(, datagram.size(), QHostAddress(""), 45454);@
    Multicast Receiver:
    @udpSocket->bind(QHostAddress::AnyIPv4, 45454, QUdpSocket::ShareAddress);

    (I think) multicast uses special addresses not affected with IP masks. I'm not sure which must be the correct behaviour.

    Not sure if I answered your questions :P

    Thanks for your interest!!

  • Hi,

    I have a similar multicast problem running on a windows7 machine. The machine has multiple network interfaces. When I write a mulitcast message,

    • long noBytes = socket_.writeDatagram(buffer,qMulticastChannel,qport);*

    The message is only sent a one network interface and it's the wrong interface. Is there a way to dynamically force it to a different interface.


  • I did not found any good solution. :(
    what I've done is disable all other interfaces...

  • I found the solution to the problem of sending multicast messages on different network cards.

    I had to resolve 2 issues to get it to work.

    1. When I didn't have a bind, I was able to send multicast messages. Once I bound to an address, I was unable to send messages. The ReuseAddressHint had to be part of the BindMode, in order to get it to work.

    2. I had to manually determine which networkInterface I wanted to send on, and then associate the networkInterace with the socket.

    Below is excerpts from my code.

    void socketSetup()
    // ****** Setting ReuseAddressHint as part of bind mode ******
    res = socket_.bind(QHostAddress(host()),port().toInt(), QUdpSocket::ShareAddress|QUdpSocket::ReuseAddressHint);
    if (!res)
    cout << socket_.errorString() ;

        connect(this, SIGNAL(writeReady()), this, SLOT(writeReady()));


    void writeReady()
    QByteArray buffer(writeBuffer.c_str());
    QHostAddress qAddress(channel());
    quint16 qport = port().toUShort();
    QList<QNetworkInterface> netInterfaces = QNetworkInterface::allInterfaces();
    bool noInterfaceFlg=true;

    // **** Find the network interface associated with an IP Address  ****

    // **** and associating socket with network interface. *****
    for (int interCnt = 0; noInterfaceFlg && interCnt < netInterfaces.size(); interCnt++)
    // Traverse through ever network interface

        QList <QNetworkAddressEntry> netAddress( netInterfaces[interCnt].addressEntries());
        // Traverse through every address on the interface.
        for (int addressCnt = 0; noInterfaceFlg && addressCnt < netAddress.size(); addressCnt++) 
            if (netAddress[addressCnt].ip().toString() == host())
                // found the network interface associated with an IP address
    if (noInterfaceFlg)
        // No network interface associated with ip Address
    long noBytes = socket_.writeDatagram(buffer,qAddress,qport);


Log in to reply

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