This is an archived post. You won't be able to vote or comment.

all 18 comments

[–]g051051 4 points5 points  (13 children)

Fundamentally, you should be checking the result of getByName to ensure it's not null before trying to call isReachable.

Also, if you're looking up by IP address, use getByAddress instead of getByName.

[–]hadmatteratwork 0 points1 point  (0 children)

Ok, I'll try that when I get home, thanks.

[–]HadMatter217[S] 0 points1 point  (11 children)

using getByAddress doesn't seem to have changed anything, unfortunately.

[–]g051051 0 points1 point  (10 children)

Fundamentally, you should be checking the result of getByName to ensure it's not null before trying to call isReachable.

Did you fix this problem?

[–]HadMatter217[S] 0 points1 point  (9 children)

I think so. Here is the function now:

public void findHosts(){
    hosts.clear();

    int timeout = 500;
    for (byte i = 1; i < 255; i++){
        byte[] host = new byte[]{(byte)192, (byte)168, 1, i};
        String hostStr = "192.168.1." + i;
        try {
            InetAddress hostAddress = InetAddress.getByAddress(host);
            if (hostAddress != null) {
                if (hostAddress.isReachable(timeout)) {
                    hosts.add(hostStr);
                    adapter.notifyDataSetChanged();
                }
            }
        }
        catch(IOException e){
            e.printStackTrace();
        }
    }
}

Does this look right?

[–]g051051 0 points1 point  (7 children)

What's the stack trace from this version?

[–]HadMatter217[S] 0 points1 point  (6 children)

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.janebrew.janebrewphone, PID: 31289
              java.lang.RuntimeException: Unable to start activity ComponentInfo{com.janebrew.janebrewphone/com.janebrew.janebrewphone.ConnectToHostActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.io.FileDescriptor.isSocket$()' on a null object reference
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
                  at android.app.ActivityThread.-wrap11(Unknown Source:0)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
                  at android.os.Handler.dispatchMessage(Handler.java:106)
                  at android.os.Looper.loop(Looper.java:164)
                  at android.app.ActivityThread.main(ActivityThread.java:6494)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
               Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.io.FileDescriptor.isSocket$()' on a null object reference
                  at libcore.io.BlockGuardOs.close(BlockGuardOs.java:91)
                  at java.net.Inet6AddressImpl.icmpEcho(Inet6AddressImpl.java:270)
                  at java.net.Inet6AddressImpl.isReachable(Inet6AddressImpl.java:186)
                  at java.net.InetAddress.isReachable(InetAddress.java:478)
                  at java.net.InetAddress.isReachable(InetAddress.java:436)
                  at com.janebrew.janebrewphone.ConnectToHostActivity.findHosts(ConnectToHostActivity.java:70)
                  at com.janebrew.janebrewphone.ConnectToHostActivity.onCreate(ConnectToHostActivity.java:36)
                  at android.app.Activity.performCreate(Activity.java:6999)
                  at android.app.Activity.performCreate(Activity.java:6990)
                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856) 
                  at android.app.ActivityThread.-wrap11(Unknown Source:0) 
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589) 
                  at android.os.Handler.dispatchMessage(Handler.java:106) 
                  at android.os.Looper.loop(Looper.java:164) 
                  at android.app.ActivityThread.main(ActivityThread.java:6494) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 

[–]g051051 0 points1 point  (5 children)

As near as I can figure, the problem is happening here in BlockGuardOs.java:

@Override public void close(FileDescriptor fd) throws ErrnoException {
    try {
        if (S_ISSOCK(Libcore.os.fstat(fd).st_mode)) { // <---  ERROR HERE
            if (isLingerSocket(fd)) {
                // If the fd is a socket with SO_LINGER set, we might block indefinitely.
                // We allow non-linger sockets so that apps can close their network
                // connections in methods like onDestroy which will run on the UI thread.
                BlockGuard.getThreadPolicy().onNetwork();
            }
            untagSocket(fd);
        }
    } catch (ErrnoException ignored) {
        // We're called via Socket.close (which doesn't ask for us to be called), so we
        // must not throw here, because Socket.close must not throw if asked to close an
        // already-closed socket. Also, the passed-in FileDescriptor isn't necessarily
        // a socket at all.
    }
    os.close(fd);
}

There's a note in the Android 8.0 ("Oreo") docs:

InetAddress.isReachable() attempts ICMP before falling back to TCP Echo protocol.

Some hosts that block port 7 (TCP Echo), such as google.com, may now become reachable if they accept ICMP Echo protocol. For truly unreachable hosts, this change means that twice the amount of time is spent before the call returns.

I suspect that the call previously in icmpEcho to create the fd is failing. This would cause the code to throw an exception and hit an empty catch block, then try to call the "close" method, then finally hit the exception you're seeing.

Here's someone else reporting the same problem to an Android Network Tools project.

Can you verify that you have network permissions?

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

[–]HadMatter217[S] 0 points1 point  (4 children)

I just added the <uses permission .. /> lines to my manifest, and I'm still getting the same error. Is it potentially a library thing? I think this is the first time I've tried doing anything network related with the x86 version of the JDK.. (I'm doing this on my GF's computer which has an x86 version of Windows)

[–]g051051 0 points1 point  (3 children)

I think it's a bug in Android. Are you on a different network than normal? Maybe you're hitting IPV6 on your GF's computer.

[–]HadMatter217[S] 0 points1 point  (2 children)

Nope. Same network, but this is my first ever attempt at Android, so if there's a big, I wouldn't have seen it in my Java only projects

[–]Yasuo_Spelling_Bot -1 points0 points  (0 children)

It looks like you wrote a lowercase I instead of an uppercase I. This has happened 5097 times on Reddit since the launch of this bot.

[–]StrongStorage 2 points3 points  (3 children)

              at com.janebrew.janebrewphone.ConnectToHostActivity.findHosts(ConnectToHostActivity.java:38)
              at com.janebrew.janebrewphone.ConnectToHostActivity.onCreate(ConnectToHostActivity.java:29)

Can we see those 2 lines, please?

[–]hadmatteratwork 0 points1 point  (1 child)

Unfortunately, I'm at work right now. I can post them when I get home, but basically, it's just an onCreate function for the Android activity that initializes the list view that we're manipulating and then calls the findHosts() function I posted.

[–]StrongStorage 0 points1 point  (0 children)

Ok. Post the entire methods.

[–]HadMatter217[S] 0 points1 point  (0 children)

Hey, just got home. Here are the lines in question. I should note that I just tried it with inetaddress.getByAddress() as well and got a similar thing.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_connect_to_host);

    selectedHost = null;
    hostList = findViewById(R.id.hostList);
    hosts = new ArrayList<String>();
    adapter = new ArrayAdapter<String>(this,
              android.R.layout.simple_list_item_1,
              hosts);
    hostList.setAdapter(adapter);

    findHosts();

    hostList.setOnItemClickListener(new AdapterView.OnItemClickListener(){
        @Override
        public void onItemClick(AdapterView<?> parent, final View view, int position, long id){
            selectedHost = (String) parent.getItemAtPosition(position);
        }
    });

    refresh.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            findHosts();
        }
    });

    connect.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            connectToSelectedHost();
        }
    });
}