首页 > 解决方案 > InetAddress.getAllByName() 被阻塞

问题描述

我最近遇到了一个有线问题。我有一个连接wifi的android设备,我确定wifi很好。但安卓设备无法访问互联网。然后我发现DNS解析有问题,这就是我所做的。

1. 我写了一个应用程序,它只调用InetAddress.getAllByName("www.google.com")MainActivity 中的方法。

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

    new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                Log.d("DNSTEST", "here >>> 0");
                InetAddress[] addresses = InetAddress.getAllByName("www.google.com");
                Log.d("DNSTEST", "here >>> 1");
                if (addresses != null) {
                    Log.d("DNSTEST", "here >>> 2");
                    for (InetAddress address : addresses) {
                        Log.d("DNSTEST", "here >>> 3");
                        Log.d("DNSTEST", "address: " + address.getHostAddress());
                    }
                }
                Log.d("DNSTEST", "here >>> 4");
            } catch (UnknownHostException e) {
                e.printStackTrace();
            }
        }
    }).start();
}

这是 logcat 输出:

2019-05-27 11:14:36.710 5204-5221/com.upward.dns D/DNSTEST: here >>> 0

如您所见,它在执行时被阻止InetAddress.getAllByName("www.google.com")


2.然后我用C++写了一个可执行程序试试看直接在android上执行时是否被阻塞。

/*
 * dns.cpp
 *
 *  Created on: May 27, 2019
 *  Author: Will Tang
 */
#include<cstdio>
#include<netdb.h>
#include<arpa/inet.h>

int main()
{
    printf("here >>> 0\n");
    struct hostent *hptr = gethostbyname("www.google.com");

    printf("here >>> 1\n");
    char** pptr = hptr->h_addr_list;

    printf("here >>> 2\n");
    char str[32] = {0};

    printf("here >>> 3\n");
    for (; *pptr != nullptr; pptr++) {
        printf("here >>> 4\n");
        printf("address: %s\n", inet_ntop(hptr->h_addrtype, *pptr, str, sizeof(str)));
    }

    printf("done\n");
}

将源代码制作成可执行程序'DNS',然后将其推送到android sd卡目录中,然后执行它。 在此处输入图像描述

没关系!


3.然后我用相同的C++代码写了一个共享库,并使用JNI让java调用C++代码。

#include "com_upward_dns_DNSTest.h"
#include <android/log.h>
#include <thread>
#include <netdb.h>
#include <arpa/inet.h>

#define TAG "DNSTEST"
#define printf(...) __android_log_print(ANDROID_LOG_DEBUG, TAG, __VA_ARGS__)


JNIEXPORT void JNICALL Java_com_upward_dns_DNSTest_test(JNIEnv *env, jobject obj, jint count)
{
    printf("here >>> 0");
    struct hostent *hptr = gethostbyname("www.google.com");

    printf("here >>> 1");
    char** pptr = hptr->h_addr_list;

    printf("here >>> 2");
    char str[32] = {0};

    printf("here >>> 3");
    for (; *pptr != nullptr; pptr++) {
        printf("here >>> 4");
        printf("address: %s\n", inet_ntop(hptr->h_addrtype, *pptr, str, sizeof(str)));
    }

    printf("done");
}

package com.upward.dns;

public class DNSTest {

    static {
        System.loadLibrary("dns");
    }

    public native void test();

}
2019-05-27 11:41:18.694 5860-5860/com.upward.dns D/DNSTEST_JNI: here >>> 0

坏消息,它被封锁了。谁能给我一些建议?谢谢!

标签: javaandroidc++dnsinetaddress

解决方案


推荐阅读