My C skills are a little rusty, so here is a counter example written in Python. My local IPv4 address is 37.77.56.75, so I will bind. I tried to focus on concepts as simple as possible.
This is the server side:
Here we bind to the IPv6 address in the code, but the address is actually the IPv6 mapped IPv4 address, so we actually bind to the IPv4 address. This can be seen by looking at ie netstat:
$ netstat -an | fgrep 5000 tcp4 0 0 37.77.56.75.5000 *.* LISTEN
Then we can use the IPv4 client to connect to this server:
And the server will show us who is connected using the IPv6 address view:
('::ffff:37.77.56.76', 50887, 0, 0)
In this example, I connected from the IPv4 host 37.77.56.76 , and it selects port 50887 to connect.
In this example, we are only listening on an IPv4 address (using IPv6 sockets, but it is still an IPv4 address), so clients with IPv6 support will not be able to connect. A client with IPv4 and IPv6 could, of course, use IPv6 sockets with IPv6-mapped IPv4 addresses, but then it really would not use IPv6, but simply an IPv6 representation of an IPv4 connection.
A two-stack server must either:
- listening to a wildcard address that will force the OS to accept connections on any address (both IPv4 and IPv6)
- listen for both the IPv6 address and the IPv4 address (either by creating an IPv4 socket, or by creating an IPv6 socket, and listening to the IPv6-mapped-IPv4 address, as shown above).
Using a wildcard address is the easiest. Just use the server example above and replace the host name:
The "My Mac OS X" field shows this as:
$ netstat -an | fgrep 5000 tcp46 0 0 *.5000 *.* LISTEN
Pay attention to tcp46 , which indicates that it is listening on both address families. Unfortunately, on Linux, it only shows tcp6 even when listening on both families.
Now for the most complex example: listening to multiple sockets.
When running this example, two sockets are displayed:
$ netstat -an | fgrep 5000 tcp6 0 0 2a00:8640:1::224.5000 *.* LISTEN tcp4 0 0 37.77.56.75.5000 *.* LISTEN
And now IPv6-only clients can connect to 2a00:8640:1::224:36ff:feef:1d89 , and IPv4-only clients can connect to 37.77.56.75 . Dual stack clients can choose which protocol they want to use.