QUdpsocket send and receive broadcast in linux
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
Code:
iface = "";
if(nf.name() == iface || iface.isNull() || iface.isEmpty())
{
{
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
#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 qFatal("bind udp");
if(verbose)
qDebug() << "configured, hostname=" << hostname;
}
loop for listening
Code:
int handleUdp() {
if(!udpsock.hasPendingDatagrams()) return -1;
char data[1024];
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
Re: QUdpsocket send and receive broadcast in linux
Why do you think you should bind(line 34) to a broadcast address (line 31) on the Linux machine?
Re: QUdpsocket send and receive broadcast in linux
Check the port number.
Maybe you are accidentally trying to bind to a port < 1024
Cheers,
_
Re: QUdpsocket send and receive broadcast in linux
Quote:
Originally Posted by
ChrisW67
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 and see in other opensource too, however binding to IP address dont resolve problem anyway.
I listen to a port bigger than 5000
Re: QUdpsocket send and receive broadcast in linux
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
Re: QUdpsocket send and receive broadcast in linux
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.