views:

1049

answers:

4

I have a fairly simple game that works perfectly on every version now up through 2.1, but with the new 2.2 (Froyo) release I am unable to create a socket. I am using the mina package for nio, and get this exception:

W/System.err( 263): java.net.SocketException: Bad address family W/System.err( 263): at org.apache.harmony.luni.platform.OSNetworkSystem.connectStreamWithTimeoutSocketImpl(Native Method) W/System.err( 263): at org.apache.harmony.luni.platform.OSNetworkSystem.connect(OSNetworkSystem.java:115) W/System.err( 263): at org.apache.harmony.nio.internal.SocketChannelImpl.connect(SocketChannelImpl.java:272) W/System.err( 263): at org.apache.harmony.nio.internal.PipeImpl$SinkChannelImpl.finishConnect(PipeImpl.java:164) W/System.err( 263): at org.apache.harmony.nio.internal.PipeImpl.(PipeImpl.java:48) W/System.err( 263): at org.apache.harmony.nio.internal.SelectorProviderImpl.openPipe(SelectorProviderImpl.java:51) W/System.err( 263): at org.apache.harmony.nio.internal.SelectorImpl.(SelectorImpl.java:141) W/System.err( 263): at org.apache.harmony.nio.internal.SelectorProviderImpl.openSelector(SelectorProviderImpl.java:58) W/System.err( 263): at java.nio.channels.Selector.open(Selector.java:48) W/System.err( 263): at org.apache.mina.transport.socket.nio.SocketConnector.startupWorker(SocketConnector.java:248) W/System.err( 263): at org.apache.mina.transport.socket.nio.SocketConnector.connect(SocketConnector.java:210) W/System.err( 263): at org.apache.mina.transport.socket.nio.SocketConnector.connect(SocketConnector.java:137) W/System.err( 263): at org.apache.mina.common.support.BaseIoConnector.connect(BaseIoConnector.java:40)

Later in the log, usually immediately following I get this:

W/System.err( 263): java.lang.NullPointerException W/System.err( 263): at org.apache.harmony.nio.internal.SelectorImpl.wakeup(SelectorImpl.java:418) W/System.err( 263): at org.apache.mina.transport.socket.nio.SocketConnector.connect(SocketConnector.java:222) W/System.err( 263): at org.apache.mina.transport.socket.nio.SocketConnector.connect(SocketConnector.java:137) W/System.err( 263): at org.apache.mina.common.support.BaseIoConnector.connect(BaseIoConnector.java:40)

I have done all the googling and looking around I can think of and found nothing. The closest I have come seems to be an old JDK bug with ipv6 support on XP and Vista machines (I'm running Vista). Recommendations included disabling ipv6 (that did not work) and disabling ipv4 and leaving ipv6 (will not work for me as my router and ISP don't support it and so could not test anyway).

Any thoughts, suggestions, things I have not tried?

Thanks, Josh

A: 

Hi. I have same problem & same logs. (selector open fail on Android 2.2) Did you find some answer? My environment is not VISTA and as the logs I just open NIO selector not connecting address.

rednos
Sorry, no, I never found an answer. As I noted in my comment above, I decided to re-write my socket code using standard Socket calls instead which resolved the issue. What I can say for sure is that it has something to do with IPV6 when doing NIO, but I got no further.
Josh
A: 

Yes, You are right. I posted this problem to google groups and the comment was "the emulator doesn't support IPv6". Here is the link http://code.google.com/p/android/issues/detail?id=9431

Do you know why application should think it's device can support iPv6 or not? I think java application programmers don't need to know that.

rednos
+5  A: 

Thanks to Josh... I could get the andswer for this problem at the link above

using this code before opening a Selector

==> System.setProperty("java.net.preferIPv6Addresses", "false");

i can pass the problem...

gustyJin
Works for me as well.
miracle2k
Works for me. My environment is emulator running on Ubuntu 10.4 system.
Tushar Tarkas
this is a dangerous workaround on real devices. please vote up _my_ answer ;-)
Elliott Hughes
+1  A: 

this was a bug and has been fixed: http://code.google.com/p/android/issues/detail?id=9431

you should be very careful with the java.net.preferIPv6Addresses workaround, because there are devices and networks where you do want IPv6.

(and, as the original poster found, if you can use io rather than nio, you generally should.)

Elliott Hughes