0

I am new with the network programming and I have a few questions, that I couldn't find anywhere. I don't understand if there is a difference in code between IPv4 and IPv6, when establishing connection.

Example Code : Socket socket = new Socket(“127.0.0.1”, 5000)

The above code is used for IPv4, as I understood. But how do I initialize the socket if I want to use IPv6?

Chris Maggiulli
  • 3,375
  • 26
  • 39
ariaz
  • 3
  • 3

1 Answers1

0

I don't understand if there is a difference in code between ipv4 and ipv6, when establishing connection.

There is little difference.

  • If you want to use an explicit IPv6 address, you will typically just instantiate the Socket with a IP address string in IPv6 syntax.

  • If you use a DNS name, then the available network stacks will determine whether you use IPv4 or IPv6:

    • If only one stack is supported (by the OS) and available, that is used.
    • If both stacks are available, the setting of the java.net.preferIPv4Stack property determines which is used.

For more information, read Networking IPv6 User Guide from the Oracle Java documentation.

For example this: Socket socket = new Socket("127.0.0.1", 5000) is used for ip4, as I understood. But how do I initialize the socket if I want to use ip6?

Socket socket = new Socket("::1", 5000);

See also: What is IPV6 for localhost and 0.0.0.0?

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216