A strange IP Address in QUdpSocket
-
Hi,
I have two simple apps ( sender and receiver ), which are executed on two different computers. That computers have ip addresses:
- sender pc: 192.168.0.30
- receiver pc: 192.168.0.45
And codes:
Sender:socket = new QUdpSocket(this); QByteArray byteArray = "helloWorld"; socket->bind(QHostAddress::Any, 12353); socket->writeDatagram(byteArray,byteArray.size(),QHostAddress("192.168.0.45"),12359);
Receiver:
socket->bind(QHostAddress::Any, 12359); connect(socket, &QUdpSocket::readyRead, this, &QMainWindow::readyReadSlot); And in readyReadSlot: while(socket->hasPendingDatagrams()) { QNetworkDatagram datagram = socket->receiveDatagram(); QByteArray array = datagram.data(); std::cout<<"Port: "<<datagram.senderPort()<<" Address: "<<datagram.senderAddress().toString().toStdString().c_str()<<" Protocol: "<<datagram.senderAddress().protocol()<<std::endl; }
And on cout I get informations:
Port: 12353 Address: ::ffff:192.168.0.30 Protocol: 1
Protocol 1 is IPv6 ( https://doc.qt.io/qt-5/qabstractsocket.html#NetworkLayerProtocol-enum )
But what is::ffff:192.168.0.30
? This is a strange string which is concatenation IPv4 address ( 192.168.0.30 ) and IPv6 address ( ::ffff: ). And why sender send datagram using IPv6? In writeDatagram I wrote "192.168.0.45" and this is IPv4 AddressOf course in receiver I receive "helloWorld".
-
@qwe3 said in A strange IP Address in QUdpSocket:
But what is ::ffff:192.168.0.30? This is a strange string which is concatenation IPv4 address ( 192.168.0.30 ) and IPv6 address ( ::ffff: ).
That is the RFC5952 format for a special IPv6 address where the IPv4 address is the last four octets. These addresses are usually associated with tunneling IPv4 inside IPv6. For readability these four octets can be displayed in decimal as is convention in IPv4.
And why sender send datagram using IPv6? In writeDatagram I wrote "192.168.0.45" and this is IPv4 Address
You have asked the operating system to deliver a datagram to an IPv4 address. That does not obligate the OS, or the network in between, to use an IPv4 interface to achieve that. It seems your environment has a preference.
On the receiving end, you have have called bind() in a way that should respond to a datagram on any IPv4 or IPv6 interface (QHostAddress::Any). What happens if you bind() to QHostAddress::AnyIPv4?