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:
http://social.technet.microsoft.com/Forums/en-US/w7itpronetworking/thread/72e7387a-9f2c-4bf4-a004-c89ddde1c8aa/
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?
thaaaanks!
-
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":http://qt-project.org/doc/qt-5.0/qtnetwork/multicastreceiver.html / "multicast sender":http://qt-project.org/doc/qt-5.0/qtnetwork/multicastsender.html and "broadcast receiver":http://qt-project.org/doc/qt-5.0/qtnetwork/broadcastreceiver.html / "broadcast sender":http://qt-project.org/doc/qt-5.0/qtnetwork/broadcastsender.html.But, the relevant code is:
Broadcast Sender:
@udpSocket->writeDatagram(datagram.data(), datagram.size(), QHostAddress::Broadcast, 45454);@
Broadcast Receiver:
@udpSocket->bind(45454, QUdpSocket::ShareAddress);@
broadcast address allways is 255.255.255.255 (http://qt-project.org/doc/qt-5.0/qtnetwork/qhostaddress.html)
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.data(), datagram.size(), QHostAddress("239.255.43.21"), 45454);@
Multicast Receiver:
@udpSocket->bind(QHostAddress::AnyIPv4, 45454, QUdpSocket::ShareAddress);
udpSocket->joinMulticastGroup(groupAddress);@(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.
Thanks,
Mark -
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.
-
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.
-
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() ;
return;
}socket_.setSocketOption(QAbstractSocket::MulticastTtlOption,"1"); socket_.setSocketOption(QAbstractSocket::MulticastLoopbackOption,"0"); connect(this, SIGNAL(writeReady()), this, SLOT(writeReady()));
}
void writeReady()
{
QByteArray buffer(writeBuffer.c_str());
writeBuffer.erase(buffer.size());
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 interfaceQList <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 socket_.setMulticastInterface(netInterfaces[interCnt]); noInterfaceFlg=false; } } } if (noInterfaceFlg) { // No network interface associated with ip Address return; } long noBytes = socket_.writeDatagram(buffer,qAddress,qport);
}
@ -