15

Short and simple question: I am new to boost::asio and I was wondering if it is possible to create a tcp::acceptor listening for both, IPv4 and IPv6 connections together. The tutorials on boost's homepage show something like this:

_acceptor = new tcp::acceptor(_ioService, tcp::endpoint(tcp::v4(), 3456));

where the endpoint is always specified with a specific protocol. Is it not possible to listen for IPv4 and IPv6 on the same port at the same time?

Mateusz Piotrowski
  • 8,029
  • 10
  • 53
  • 79
PuerNoctis
  • 1,364
  • 1
  • 15
  • 34

1 Answers1

17

If you create a IPv6 acceptor, it will accept both IPv4 and IPv6 connections if IPV6_V6ONLY socket option is cleared. IPv4 addresses will be presented as IPv6 addresses, in the IPv4-mapped format.

Problems arise mainly around whether IPV6_V6ONLY is available or what the default value is (turned on or off). So I find it's better to set it explicitly to what you want.

Also Windows XP doesn't support the option at all.

So if you want to be compatible across systems, it's recommended to create two sockets, one for v4 and one for v6 setting IPV6_V6ONLY.

Shane Powell
  • 13,698
  • 2
  • 49
  • 61
  • That's exactly what I was looking for. I indeed need some compatibility across systems, most importantly Windows 7 or higher and linux, whereas I have seen that IPV6_V6ONLY is supported there since around kernel 2.6. XP/Server2003 shouldn't be a problem, but it would be nice to somehow check for all available socket options at runtime - sadly I didn't come up with a way to do that, it's all preprocessor defines as far as I can tell (or I try to set the option and check for a binding error). Anyways, this answers my question. – PuerNoctis Jun 30 '15 at 07:50