sharajava
sharajava

Reputation: 31

Java: Can socket connect to IPv6 address while binding a IPv4 address?

InetSocketAddress ipv4 = ...; // the IPv4 address
InetSocketAddress ipv6 = ...; // the IPv6 address

Socket sock = ...; // initialize a socket

sock.bind(ipv4);
sock.connect(ipv6, 0);

Both sides are IPv4/IPv6 dual stack support.

Can Java support for this kind of connection?

If the JDK could try to translate the binding address into a IPv6 one or translate the target address into a IPv4 one?

I tried case of bind IPv6 while connect to IPv4. JDK translate the binding IPv6 address into a IPv4 one during the connecting process. But when trying bind IPv4 while connect to IPv6 (as I mentioned above), I just got time out exception. I'm not sure if this is not support or some issue of my environment. I guess there should be some exception indicating not support thrown before connecting, not a time out finally.

Any idea? Thanks.

Upvotes: 1

Views: 2171

Answers (2)

user4438328
user4438328

Reputation: 35

It is possible to create separate Sockets listen on IPv4 and IPv6 on unix machines. http://msdn.microsoft.com/en-us/library/windows/desktop/bb513665%28v=vs.85%29.aspx

Upvotes: -1

Sander Steffann
Sander Steffann

Reputation: 9978

IPv4 and IPv6 are different protocols. You cannot mix them. You can only connect from an IPv4 address to another IPv4 address or from an IPv6 address to another IPv6 address.

Upvotes: 3

Related Questions