0
我想用我的應用程序掃描我的本地網絡,所以我試圖ping所有本地IP,但我的代碼只適用於我的android設備的本地IP。Ping不起作用
這是我的函數:
int timeout=10000;
for (int i = 0; i < 256; i++) {
String host = "192.168.1." + i;
try {
if (InetAddress.getByName(host).isReachable(timeout)) {
System.out.println(host + " is reachable");
Log.i("host", host);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
輸出: 「主持人:192.168.1.10」 這是我的設備的本地IP。
我的清單:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="...">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Oups,在我的代碼中點是存在的。 (編輯) – Error404
@ Error404,那麼一切都應該工作。我只是測試了代碼。它正在我的電腦上工作。 –
int timeout = 1000;/*我爲(int i = 0; i <256; i ++)減少了超時值*/ String host =「192.168.1。」 +我; (InetAddress.getByName(host).isReachable(timeout))System.out.println(host +「is reachable」);如果(InetAddress.getByName(host).isReachable(timeout)){0}。 (IOException e){ } } catch } } –