Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IPv6 Socket on Android

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?

like image 942
Steven Meliopoulos Avatar asked Nov 16 '12 17:11

Steven Meliopoulos


1 Answers

Since your IP Address is link local, perhaps it MUST be created with a scope ID or Interface so that the OS knows what interface to bind it to. As for specifying the scope_id, the Android docs seem somewhat lacking. The Oracle docs here indicate that the scope ID is system specific and cannot be determined programmatically.

So, I would try using the alternate factory method where you explicitly specify the NetworkInterface you wish to use. See here for details. There are methods in the NetworkInterface class to discover what interfaces exist on the system.

like image 170
schtever Avatar answered Sep 25 '22 19:09

schtever