我試過這個程序來查找系統所連接子網中的所有up ip。
package com.Server;
import java.io.IOException;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.InterfaceAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Enumeration;
import com.Client.Client;
import com.Main.Data;
public class Subnet
{
public void Subnet() throws UnknownHostException, SocketException
{
Enumeration e = NetworkInterface.getNetworkInterfaces();
Data dt = new Data();
Client cl = new Client();
dt.setClientObject(cl);
while(e.hasMoreElements())
{
NetworkInterface n = (NetworkInterface) e.nextElement();
Enumeration ee = n.getInetAddresses();
while (ee.hasMoreElements())
{
InetAddress i = (InetAddress) ee.nextElement();
String ip = i.getHostAddress();
String sip = ip.substring(0, ip.indexOf('.',ip.indexOf('.',ip.indexOf('.')+1) + 1) + 1);
try {
for(int it=1;it<=255;it++)
{
sip = sip+it;
InetAddress.getByName(sip).isReachable(100);
System.out.println(sip+" is online");
cl.ask(sip);
}
} catch (IOException e1) {
System.out.println(sip);
}
}
}
}
}
沒有用。 –
確保您的調制解調器不屏蔽其他IP。 –
未找到包'com.Client'! ,你是否發佈了完整的代碼? –