I'm on Linux with QT 4.8.
I would like that 2 or more processes on the same machine could listen on the same port for UDP datagrams. Qt documentation says that QUdpSocket::ShareAddress can do that.
I have implemented 2 receivers with this code:
connect(&rec, SIGNAL(readyRead()), this, SLOT(leggiDati()));
rec.bind(QHostAddress::any, 5000, QUdpSocket::ShareAddress);
the "leggiDati()" function:
while(rec.hasPendingDatagrams()){
QByteArray dato;
dato.resize(rec.pendingDatagramSize());
rec.readDatagram(dato.data(), dato.size(), &sender, & port);
}
and the sender code:
QUdpSocket send;
QByteArray dato = "prova invio";
send.writeDatagram(dato.data(), dato.size(), QHostAddress::LocalHost, 5000);
With the previous code, only the last process started receives the datagram. if I send a broadcast datagram, instead of on localhost, both processes receive the message.
can anyone explain to me why this happens? Is there any way to permit communications on localhost avoiding to send broadcast messages?