Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED]Determine which ip has received a UDP datagram
Qt 6.11 is out! See what's new in the release blog

[SOLVED]Determine which ip has received a UDP datagram

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 2.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    antonyprojr
    wrote on last edited by
    #1

    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:

    @QUdpSocket *socket;

    Server::Server(QObject *parent) :
    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;
    buffer.resize(socket->pendingDatagramSize());

    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;
    }@

    1 Reply Last reply
    0
    • C Offline
      C Offline
      ChrisW67
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • A Offline
        A Offline
        antonyprojr
        wrote on last edited by
        #3

        Thanks for your reply I will do that.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved