0
自分のアプリケーションでローカルネットワークをスキャンしたいので、すべてのローカルIPに対してpingを実行しようとしましたが、私のコードは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、すべてがうまくいくはずです。私はコードをテストしました。それは私のPCで働いています。 –
int timeout = 1000;/*タイムアウト値を減らしました*/ for(int i = 0; i <256; i ++){ String host = "192.168.1" + i; try { if(InetAddress.getByName(host).isReachable(timeout)){ System.out.println(host + "到達可能"); } } catch(IOException e){ e.printStackTrace(); } } –