UDP Socket - writeDatagram uses IPv6 and not IPv4
-
I'm having this problem that when I write a datagram like
writeDatagram(data, QHostAddress("127.0.0.1"), 600)
, for some reason it's using IPv6. I checked this when I got a datagram, and it does indeed say IPv6 (QAbstractSocket::NetworkLayerProtocol(IPv6Protocol)
). The sending address somehow becomes::ffff:127.0.0.1
This is a problem since the data sent I'm specify who it was from, so it does not add up since it's compating a IPv6 address to a IPv4 one. A fix would be stripping the IPv6 part off from the address when comparing, but that doesn't like a real solution, just a "hack".
I've found that I can specify the protocol used with connectToHost, but I have some other strange problem using that, it doesn't even connect for me. My solution/workaround for that was sending the data directly with writeDatagram as seen above.
Any ideas?
-
@mikkel1156 said in UDP Socket - writeDatagram uses IPv6 and not IPv4:
I've found that I can specify the protocol used with connectToHost, but I have some other strange problem using that, it doesn't even connect for me
Are you sure the underlying OS where you run this application is indeed implementing IPv4?
-
@Pablo-J.-Rogina said in UDP Socket - writeDatagram uses IPv6 and not IPv4:
@mikkel1156 said in UDP Socket - writeDatagram uses IPv6 and not IPv4:
I've found that I can specify the protocol used with connectToHost, but I have some other strange problem using that, it doesn't even connect for me
Are you sure the underlying OS where you run this application is indeed implementing IPv4?
I don't know. I'm developing this on Windows. If there is a way to set the implementation to IPv4, then that will probably fix it. I don't know of such though.
-
@mikkel1156 open "Network and Sharing Center", on the left "Change adapter settings", locate the adapter (i.e. Local Area Connection), right click Properties. A dialog opens showing items used by the connection. You should see "Internet Protocol Version 6" and hopefully "Internet Protocol Version 4"
-
I guess this is a good enough solution. Thanks for the help.
-
I saw this thread when I Googled for a solution to the same "problem". The proposed solution here is not good.
A better method is to bind your socket to an IPv4 address...
QUdpSocket socket; socket.bind(QHostAddress("0.0.0.0"), 0);
this will bind to INADDR_ANY (IPv4) at some ephemeral port (if you don't care about local port).
-
@poncho524 said in UDP Socket - writeDatagram uses IPv6 and not IPv4:
QHostAddress("0.0.0.0")
Then why not use
QHostAddress::AnyIPv4
? Looks more clear to me...