我正在尝试在两个Android设备之间创建IPv6 TCP连接。但是,创建套接字总是失败。
如果我像这样实例化它:
Inet6Address dest = (Inet6Address) InetAddress.getByName(addressString);
Socket socket = new Socket(dest, portNumber);
我得到以下异常:
java.net.ConnectException: failed to connect to *address* (port *portNumber*): connect failed: EINVAL (Invalid argument)
如果我改为实例化我的IPv6Address对象:
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);
调用Socket构造函数时出现此错误:
java.net.ConnectException: failed to connect to *address* (port *portNumber*): connect failed: EADDRNOTAVAIL (Cannot assign requested address)
这种情况发生在带有果冻豆的Galaxy Nexus和带有姜饼的Nexus One上。
我做错了吗?创建这样的套接字的正确方法是什么?
另外:This post建议使用构造函数
Inet6Address getByAddress (String host, byte[] addr, int scope_id)
在这种情况下,我必须使用什么作为scope_id?