I am trying to create an IPv6 TCP connection between two Android devices. However creating the socket always fails.
If I instantiate it like this:
Inet6Address dest = (Inet6Address) InetAddress.getByName(addressString);
Socket socket = new Socket(dest, portNumber);
I get the following exception:
java.net.ConnectException: failed to connect to *address* (port *portNumber*): connect failed: EINVAL (Invalid argument)
If I instead instantiate my IPv6Address object like this:
Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
NetworkInterface wifiInterface = null;
while (networkInterfaces.hasMoreElements()) {
NetworkInterface networkInterface = networkInterfaces.nextElement();
if (networkInterface.getDisplayName().equals("wlan0") || networkInterface.getDisplayName().equals("eth0")) {
wifiInterface = networkInterface;
break;
}
}
Inet6Address dest = Inet6Address.getByAddress(null, addressBytes, wifiInterface );
Socket socket = new Socket(dest, portNumber);
I get this error when calling the Socket constructor:
java.net.ConnectException: failed to connect to *address* (port *portNumber*): connect failed: EADDRNOTAVAIL (Cannot assign requested address)
This happens both on a Galaxy Nexus with Jelly Bean and a Nexus One with Gingerbread.
Am I doing something wrong? What is the correct way to create a socket like that?
Also: This post suggests using the constructor
Inet6Address getByAddress (String host, byte[] addr, int scope_id)
What would I have to use as scope_id in this case?