我正在尝试使用java / Android检查网络中特定端口上运行的所有服务器。我正在使用的方法目前需要5-10分钟才能做到这一点。有更快的方法吗?
我是socket编程的新手,所以任何帮助都将受到高度赞赏。
public void findservers()//throws UnknownHostException, SocketException
{
Enumeration e = null;
try {
e = NetworkInterface.getNetworkInterfaces();
} catch (SocketException e1) {
e1.printStackTrace();
}
//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();
System.out.println(ip);
String sip = ip.substring(0, ip.indexOf('.',ip.indexOf('.',ip.indexOf('.')+1) + 1) + 1);
System.out.println(sip);
try {
for(int it=1;it<=255;it++)
{
String sip1 = sip+it;
System.out.println(sip1);
//InetAddress.getByName(sip1).isReachable(100);
//System.out.println(sip1+" is online");
findserver(sip1);
//cl.ask(sip);
}
} catch (Exception e1) {
System.out.println(sip);
System.out.println("hello");
}
}
}
}
public void findserver(String ip)
{
// String ip = "192.168.43.41";
try
{
InetAddress ping = InetAddress.getByName(ip);
Socket s = new Socket(ping, 50005);
System.out.println("Server found on IP: " + ping.getCanonicalHostName());
s.close();
} catch (IOException e) {
System.out.println("Nothing much");
System.out.println(e);
}
}