Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/234.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android端口扫描不工作_Java_Android_Sockets_Textview_Port Scanning - Fatal编程技术网

Java Android端口扫描不工作

Java Android端口扫描不工作,java,android,sockets,textview,port-scanning,Java,Android,Sockets,Textview,Port Scanning,我正在开发一个android端口扫描仪,但它似乎不起作用。下面是PortScan类的代码 import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.Socket; import java.net.UnknownHostException; import android.app.Activity; import android.content.Intent; import android.os.B

我正在开发一个android端口扫描仪,但它似乎不起作用。下面是PortScan类的代码

import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class PortScan extends Activity
{

    String targetHost;

    /* Starting port for scan */
    public int startPort = 1;

    /* Ending port for scan */
    public int endPort = 100;

    /* Adapter for ListView */
    //private PortScanAdapter scanAdapter;

    /* Intent which invoked this class */
    private Intent scanIntent;

    /* Address of the host to scan */
    InetAddress targetAddress;

    /* Hostname of the target */
    String targetHostName;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        scanIntent = getIntent();
        prepareScan();
        setContentView(R.layout.port_scan_result);
        startScan();
    }

    private void prepareScan()
    {
        targetHost = scanIntent.getStringExtra("targetAddress");

        /* Get the IP Address of the target */
        try
        {
            targetAddress = InetAddress.getByName(targetHost);
        }
        catch(UnknownHostException e)
        {
            e.printStackTrace();
        }

        /* Get the hostname of the target */
        try
        {
            targetHostName = targetAddress.getHostName();
        }
        catch(Exception e)
        {
            targetHostName = targetHost;    
        }

       /*TextView hostName = (TextView)findViewById(R.id.host);
        hostName.setText(targetHostName); */
    }


    private void startScan()
    {
        /* Socket to connect to the remote machine */
        Socket portSocket;

        /* Textview which displays the scanresult */
        TextView scanText = (TextView)findViewById(R.id.portscanresult);

        scanText.setText("Scanning host "+ targetHost + "\n");

        for (int i = startPort; i == endPort; i++)
        {
            try
            {
                portSocket = new Socket();
                portSocket.connect(new InetSocketAddress(targetAddress, i), 1000);
                scanText.append("Target is listening on port "+ i + ": Port Open\n");
                portSocket.close();

            }   
            catch(Exception exception)
            {
                scanText.append("Target is not listening on port "+ i + ": Port Closed\n");
            }
        }
    }


}
我正在模拟器(Android 2.3.3)上测试这段代码。我不知道问题是出在套接字连接还是TextView上。当我运行程序时,我得到的唯一输出是
for
循环开始之前的字符串,即
扫描主机+targetHost+“\n”
,然后什么也不发生

任何帮助都将不胜感激


很好地问候。。。这是行不通的

for (int i = startPort; i == endPort; i++)
第二个是条件
,如果i==endport
,并且i等于
startPort
,因为您在开始时已经说过了<代码>整数i=startPort

它必须是这样的:

for (int i = startPort; i <= endPort; i++)

for(inti=startPort;i好吧……这永远不会起作用

for (int i = startPort; i == endPort; i++)
第二个是条件
,如果i==endport
,并且i等于
startPort
,因为您在开始时说过…
int i=startPort

它必须是这样的:

for (int i = startPort; i <= endPort; i++)
for(int i=startPort;i