PDA

View Full Version : QUdpsocket send and receive broadcast in linux



danics
2nd February 2015, 12:46
I try to write a QUdpsocket for read and write on braodcast address. following code work in windows with Qt5 library but don't work on my CentOs with Qt4.6, (i haven't any error in CentOs just don't read anything)

configure qudpsocket



iface = "";

QList<QNetworkInterface> ifaces = QNetworkInterface::allInterfaces();
foreach (QNetworkInterface nf, ifaces) {
if(nf.name() == iface || iface.isNull() || iface.isEmpty())
{
if(nf.flags() & QNetworkInterface::CanBroadcast)
{
foreach (QNetworkAddressEntry na, nf.addressEntries()) {
if(na.broadcast().toString() != "") {
send_to = na.broadcast().toIPv4Address();
bind_to = na.ip().toIPv4Address();
hostname = na.ip().toString();
if (verbose)
qDebug("using address=%s broadcast=%s",
na.ip().toString().toLocal8Bit().data(), na.broadcast().toString().toLocal8Bit().data());
break;
}
}
}
}
if(hostname != "") break;
}

if(bind_to)
{
broadcast.setAddress(send_to);
#ifdef Q_OS_WIN
QHostAddress address(bind_to);
#elif defined(Q_OS_LINUX)
QHostAddress address(send_to); // dont work on centos Qt 4.7
#endif

if(!udpsock.bind(address, port_nbr, QUdpSocket::ShareAddress
| QUdpSocket::ReuseAddressHint))
qFatal("bind udp");

if(verbose)
qDebug() << "configured, hostname=" << hostname;
}

loop for listening


int handleUdp() {
if(!udpsock.hasPendingDatagrams()) return -1;
char data[1024];
QHostAddress peerAddress;

int rsize = udpsock.readDatagram(data, 1024, &peerAddress);
if(rsize <= 0) return -1;
.....
return 0;
}

while(!terminated) {
...
if(udpsock.waitForReadyRead(2000))
handleUdp();
...
// end of each loop broadcast data
udpsock.writeDatagram(data(), broadcast, port_nbr)
}


this is debug information:

using address=123.123.1.14 broadcast=123.255.255.255
configured, hostname=123.123.1.14

ChrisW67
2nd February 2015, 20:13
Why do you think you should bind(line 34) to a broadcast address (line 31) on the Linux machine?

anda_skoa
2nd February 2015, 20:37
Check the port number.
Maybe you are accidentally trying to bind to a port < 1024

Cheers,
_

danics
3rd February 2015, 05:27
Why do you think you should bind(line 34) to a broadcast address (line 31) on the Linux machine?

I read this in different forums for example here (http://stackoverflow.com/questions/23042485/python-udp-socket-bind-to-the-correct-address) and see in other opensource too, however binding to IP address dont resolve problem anyway.

I listen to a port bigger than 5000

danics
4th February 2015, 07:05
I test this code in ubuntu with Qt 5 library and it works.

so i think maybe problem is network settings of my centos, actually my centos run in a virtual machine, anybody have a suggestion of changing settings of network for broadcast messages localy?

thanks in advance

ChrisW67
4th February 2015, 11:26
If you are using a virtual machine and you want it to talk to the outside world and accept connections/UDP from the outside world then you need to choose an appropriate virtual network interface option: usually "Bridged" or something like it. On the other hand, two virtual machines connected only to a shared VM-only network should require nothing special to talk to each other. Also need to check there is no default network firewalling going on in the OS.