[SOLVED]Determine which ip has received a UDP datagram
I’m developing a software that receive UDP datagrams, I can easily determine the ip of the sender, but I can’t determine which ip that received the message (Ex.: If the message has arrived on 127.0.0.1 or 192.168.1.1). And considering that datagram was sent to the broadcast address, not even the sender of the message knows for which ip it was sent. I've tried to use "socket->localAddress().toString()" but it always return "0.0.0.0"
And here my code:
Server::Server(QObject *parent) :
socket = new QUdpSocket(this);
qDebug() << socket->errorString();
connect(socket, SIGNAL(readyRead()), SLOT(readyRead()));
QHostAddress sender; quint16 senderPort;
socket->readDatagram(buffer.data(), buffer.size(), &sender, &senderPort);
QString received = QString(buffer);
qDebug() << "Message received on IP:" << socket->localAddress().toString() << "Port:" << socket->localPort();
qDebug() << "Message from:" << sender.toString();
qDebug() << "Message Port:" << senderPort;
qDebug() << "Message:" << received;
You will probably have to bind a separate socket to each address entry of each interface returned by QNetworkConfiguration::allInterfaces().
You can map the readyRead() signal through QSignalMapper so you know which socket to read from in a common handler.
Thanks for your reply I will do that.