私は自分のコードを必要最小限にまで切り詰めました。それは非常にシンプルで簡単です。
次のコードがあります。
public ArrayList<Node> getNodes() throws IOException
{
ArrayList<Node> nodes = new ArrayList<Node>();
StringBuffer root = new StringBuffer(InetAddress.getLocalHost().getHostAddress());
while(!root.toString().endsWith("."))
root.deleteCharAt(root.length() - 1);
//^^ this code gets the ip, for ex 127.0.0.1, and trims the last number, to make it
//^^ 127.0.0. <-- see the trailing 0
for(int host = 0;host < 256; host++)
{
InetAddress address = InetAddress.getByName(root.toString() + host);
try
{
if(address.isReachable(500)) // pings the address
nodes.add(new Node(address.getHostAddress(), false));
}catch(Exception e){new Node(address.getHostAddress(), true);}
}
return nodes;
}
以下は非常に単純なノード クラスです。
public class Node
{
public Node(String address, boolean restricted)
{
this.address = address;
this.restricted = restricted;
}
public String address;
public boolean restricted;
}
getNodes() を実行するメイン コードは次のとおりです。
case 1:
System.out.println("Searching for nodes...");
NodeDetector node = new NodeDetector(); // this is the class
//where getNodes resides
ArrayList<Node> nodes = node.getNodes();
Iterator<Node> it = nodes.iterator();
while(it.hasNext())
{
System.out.println("Node: "+it.next().address);
}
System.out.println("stopped searching for nodes...");
break;
ここに私の出力があります:
Searching for nodes...
Node: 00.00.17.99
Node: 00.00.17.100
Node: 00.00.17.149
Node: 00.00.17.150 <-- this is my computer
Node: 00.00.17.154
Node: 00.00.17.156
Node: 00.00.17.254
stopped searching for nodes...
ここで問題です
携帯電話にダウンロードしたネットワーク ノード検出ツールがあり、さらに 5 つ以上のノードを検出できます。タイムアウト値を変更しようとしましたが、それでもうまくいきません。コンピューターではなく、電話のネットワーク ツールで見つかったアドレスに ping を実行すると、ping が即座に受信されて返されます。この質問は似ており、少し役に立ちましたが、まだ行き詰まっています:
私は自分のツールを Mac で実行しています。他の Mac、iPod、ルーターを拾うとうまくいくようですが、それだけです。プログラムがネットワーク上の他のデバイスを検出できないのはなぜですか?
これは、電話のネットワーク ツールから取得した出力です。
00.00.17.99 <-- SMC Networks *
00.00.17.100 <-- XEROX *
00.00.17.133 <-- My Phone (Android)
00.00.17.134 <-- Intel
00.00.17.142 <-- Apple
00.00.17.149 <-- Apple *
00.00.17.150 <-- Apple * <-- this is my computer
00.00.17.154 <-- Apple *
00.00.17.155 <-- Intel
00.00.17.156 <-- Apple *
00.00.17.158 <-- Motorola Mobility
00.00.17.254 <-- Netopia *
携帯電話のツールがコンピューターで作成しているツールと一致する場合は、* を付けます。このテストを数回実行しましたが、コンピューターと電話の両方で毎回同じ出力が得られ、テスト中にネットワークにデバイスが追加または削除されませんでした。