我想在Android上制作一个端口扫描器,而且我有点卡住了。我想查看一个端口是否在路由器/默认网关上打开,但似乎没有任何工作。我尝试使用可达,但我觉得这可能是错误的事情。Android端口扫描器
import java.net.InetAddress;
import java.net.UnknownHostException;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.DhcpInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.widget.TextView;
public class portscan extends Activity {
String targetHost;
public int startPort = 1; //(for uses in later programming)
public int endPort = 1000;
private Intent scanIntent;
InetAddress targetAddress;
String targetHostName;
WifiManager networkd;
DhcpInfo details;
public String gateway;
TextView GW;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
setContentView(R.layout.port);
GW = (TextView)findViewById(R.id.gateway);
networkd = (WifiManager) getSystemService(Context.WIFI_SERVICE);
details = networkd.getDhcpInfo();
String test = intToIp(details.gateway);
gateway = "Default Gateway: "+String.valueOf(details.gateway);
boolean isAvailable = false;
isAvailable = InetAddress.getByName(test).isReachable(80); //trying to see if port open
if (isAvailable == true) {
GW.setText("port 21 is up");
}
} catch (Exception e) {
}
}
public String intToIp(int i) { //this converts the DHCP information (default gateway) into a readable network address
return (i & 0xFF)+ "." +
((i >> 8) & 0xFF) + "." +
((i >> 16) & 0xFF)+ "." +
((i >> 24) & 0xFF);
}
看着'isReachbable(80)',我觉得有必要指出的是,该参数是'超时value',_not端口number_。但不要介意,只是不要使用它。 – 2013-03-21 20:14:02