Domanda

I am trying to send out a Proper mDNS response packet using QUdpSocket. The trouble I am having is creating the packet correctly. Could someone please show me the proper way to put together the packet.

So far this has not worked:

    QByteArray datagram;
    QDataStream out(&datagram, QIODevice::WriteOnly);
out << 0x8400; //set standard query

    out << 0; //Reply code: no error;
    out << 0; //Questions; 0
    out << 0; //Answers; 0
    out << 1; //Authoritive answers: 0
    out << 0; //Additional RR;

    QByteArray name("_home-sharing._tcp.local");
    out <<  name;
    out << 0x000c; //PTR
    out << 1;//Class: IN
    out << 1;//Cache Flush
    out << 0; //Time to Live: 0;

Then i send the datagram, any help would be appreciated. Thanks

È stato utile?

Soluzione

QDataStream encodes into a special Qt format, it doesn't format into 'raw' binary.

You will need to serialise the data yourself. I would recommend just appending into the QByteArray. For example to serialize an uint16_t in network byte order you could use a function like this:

void appendUint16NBO(QByteArray& ba, uint16_t i)
{
  ba.append(char((i >> 8) & 0xFF));
  ba.append(char(i & 0xFF));
}

You would use the function as follows:

QByteArray datagram;
appendUint16NBO(datagram, 0x0000);
appendUint16NBO(datagram, 0x8400);

The QByteArray would then contain: 00 00 84 00.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top