在Android系统中进行网络开发时,如果使用了java.nio等相关包后有可能碰到异常java.net.SocketException: Bad address family。原因是Android2.2bug不能启用IPV6协议,具体可点击http://code.google.com/p/android/issues/detail?id=9431
java.net.SocketException: Bad address family
at org.apache.harmony.luni.platform.OSNetworkSystem.connectStream
WithTimeoutSocketImpl(Native Method)
at org.apache.harmony.luni.platform.OSNetworkSystem.connect(OSNet
workSystem.java:115)
at org.apache.harmony.nio.internal.SocketChannelImpl.connect(Sock
etChannelImpl.java:272)
at org.apache.harmony.nio.internal.PipeImpl$SinkChannelImpl.finis
hConnect(PipeImpl.java:164)
at org.apache.harmony.nio.internal.PipeImpl.(PipeImpl.java:48)
at org.apache.harmony.nio.internal.SelectorProviderImpl.openPipe(
SelectorProviderImpl.java:51)
at org.apache.harmony.nio.internal.SelectorImpl.(SelectorImpl.jav
a:141)
at org.apache.harmony.nio.internal.SelectorProviderImpl.openSelec
tor(SelectorProviderImpl.java:58)
我是在使用netty的时候发生这个问题的,后在代码中添加:
1 2 3 |
static { java.lang.System.setProperty("java.net.preferIPv4Stack", "true"); java.lang.System.setProperty("java.net.preferIPv6Addresses", "false"); |
解决了问题
参考:: http://orgcent.com/android-bad-address-family-exception/ | 萝卜白菜的博客
0 条评论