Qt World Summit: Register Today!

[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 or 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 ""

    And here my code:

    @QUdpSocket *socket;

    Server::Server(QObject *parent) :
    socket = new QUdpSocket(this);
    if(!socket->bind(QHostAddress::Any, 1))
    qDebug() << socket->errorString();
    connect(socket, SIGNAL(readyRead()), SLOT(readyRead()));

    void Server::readyRead()
    QByteArray buffer;

    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.

Log in to reply