2012-04-20 3 views
0

Я разрабатываю сканер портов Android, но он, похоже, не работает. Вот код для класса PortScan.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 i.e "Scanning host "+ targetHost + "\n", а затем ничего не происходит.

Любая помощь будет принята с благодарностью.

С уважением

ответ

4

хорошо ... это никогда не будет работать ...

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

Второе условие if i==endport и я равна startPort, потому что вы сказали, так что в начале ... int i=startPort

это должно быть так:

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

Спасибо! Теперь я действительно чувствую себя идиотом: P –

 Смежные вопросы

  • Нет связанных вопросов^_^