What happens under the hood when you create a Java ServerSocket and bind wildcard "0.0.0.0" in a dual-stack host?
Today, I am busy helping a teammate to troubleshoot an issue in Kubernetes cluster. In the investigation, I found it is related to a question that I have been dying to figure out for a long time: what happens under the hood when you create a Java ServerSocket and bind wildcard "0.0.0.0", and this time, it is in a dual-stack host. For example, in my WSL, it is a dual-stack: And then I run below Java program(just for testing): After I run it in my WSL, I use the netstat command to check the listening socket: Although I am binding the "0.0.0.0" which is an IPv4 wildcard, there's a tcp6 LISTENING socket with ":::8888" which is an IPv6 wildcard! So, why? What happens!? Let's delve into it. Firstly, let's check what happens when we create an InetSocketAddress with "0.0.0.0": It will try to resolve the hostname ("0.0.0.0" in our case): InetAddress.getByName(hostname). Let's find what does getAllByName do: private static I...