I am trying to read the data send by Colasoft Packet Player via Qt.
I use a loopbackadapter with IP 192.168.1.99 and Wireshark shows me something like this:
SourceIP: 192.168.1.1
SourcePort: 40102
DestinationIP: 224.0.1.12
DestinationPort: 49156
But using those IPs and Ports, Qt QUdpSocket does not show any data. When I try to read over all ports and IPs i receive data, so the programm should work, but it seems, I do not get any data send by Colasoft Packet Player.
What do I do wrong?
Thank you!
MyUDPSocket::MyUDPSocket(QObject *parent)
: QObject(parent)
{
socket = new QUdpSocket(this);
//socket->bind(QHostAddress("127.0.0.1"), 40102);
//socket->bind(QHostAddress("127.0.0.1"), 49156);
//socket->bind(QHostAddress("224.0.1.12"), 40102);
//socket->bind(QHostAddress("224.0.1.12"), 49156);
//socket->bind(QHostAddress("192.168.1.1"), 40102);
//socket->bind(QHostAddress("192.168.1.1"), 49156);
//socket->bind(QHostAddress("192.168.1.99"), 40102);
//socket->bind(QHostAddress("192.168.1.99"), 49156);
//socket->bind(QHostAddress::Any, 40102);
//socket->bind(QHostAddress::Any, 49156);
socket->bind(QHostAddress::Any, 49156, QUdpSocket::ShareAddress);
socket->joinMulticastGroup(QHostAddress("224.0.1.12"));
connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()));
}
void MyUDPSocket::readyRead()
{
// when data comes in
QByteArray buffer;
buffer.resize(socket->pendingDatagramSize());
QHostAddress sender;
quint16 senderPort;
// qint64 QUdpSocket::readDatagram(char * data, qint64 maxSize,
// QHostAddress * address = 0, quint16 * port = 0)
// Receives a datagram no larger than maxSize bytes and stores it in data.
// The sender's host address and port is stored in *address and *port
// (unless the pointers are 0).
socket->readDatagram(buffer.data(), buffer.size(),
&sender, &senderPort);
qDebug() << "\n+-------------------------------";
qDebug() << "|Message from:" << sender.toString();
qDebug() << "|Message port:" << senderPort;
qDebug() << "|Message:" << buffer;
qDebug() << "+-------------------------------\n";
}
}
The address 224.0.1.12
corresponds to multicast.
So, you want to join multicast stream, see bool QUdpSocket::joinMulticastGroup(const QHostAddress & groupAddress)
, from that document:
Note that if you are attempting to join an IPv4 group, your socket must not be bound using IPv6 (or in dual mode, using QHostAddress::Any). You must use QHostAddress::AnyIPv4 instead.
There is also Qt Multicast Receiver Example.
Update due to original question code changes
I verified that the original code before edit (without joinMulticastGroup()
) was able to receive normal unicast UDP packets to port 49156. So, the code worked as a server listening bound port.
Now with joinMulticastGroup()
and replacement QHostAddress::Any
-> QHostAddress::AnyIPv4
the code is able to receive multicast stream from VLC player.
So, the coding issues are resolved. Other possible issues: