端口通过socket.connect扫描外部服务器

时间:2016-01-14 08:38:24

标签: java android sockets

我正在尝试使用套接字连接进行端口扫描活动,但我无法确定是否可以像我一样做或者我做错了。

我的java:

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;


public class portscan extends AppCompatActivity {
    EditText Portaa;
    EditText Portbb;
    EditText Iptot;
    TextView rez;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_portscan);
        Portaa  = (EditText)findViewById(R.id.editText3);
        Portbb = (EditText)findViewById(R.id.editText4);
        Iptot = (EditText)findViewById(R.id.editText5);
        rez = (TextView)findViewById(R.id.textView18);

    }
    private static String available(String ip, int porta, int portb) {
        String rezultat = "";
        for (int port = porta; port <= portb; port++) {
            try {
                Socket socket = new Socket();
                socket.connect(new InetSocketAddress(ip, port), 1000);
                rezultat = rezultat + "," + port;
                socket.close();


            } catch (Exception ex) {

            }

        }
        return rezultat;
    }

    public void scanclick(View s) {
        // Scaneaza
        Iptot= (EditText)this.findViewById(R.id.editText3);
        Portaa = (EditText)this.findViewById(R.id.editText4);
        Portbb = (EditText)this.findViewById(R.id.editText5);
        String portaa = Portaa.getText().toString();
        int porta = Integer.parseInt(portaa);
        String portbb = Portbb.getText().toString();
        int portb = Integer.parseInt(portbb);
        String ip = Iptot.getText().toString();
        // available(ip, porta, portb);
        //textView18
        String fina = available(ip, porta, portb);
        rez = (TextView)findViewById(R.id.textView18);
        if (fina == ""){
            rez.setText("Nici un port deschis");
        }else {
            rez.setText(fina);
        }
    }


} 

我的xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.nliplace.nli.unealta.portscan">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Aceasta activitate scaneaza dupa port-uri deschise."
            android:id="@+id/textView13"
            android:layout_gravity="center_horizontal" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Adresa:"
            android:id="@+id/textView14" />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/editText3"
            android:layout_gravity="center_horizontal" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Port A:"
            android:id="@+id/textView15" />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/editText4" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Port B:"
            android:id="@+id/textView16" />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/editText5" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Scan"
            android:id="@+id/button11"
            android:layout_gravity="center_horizontal"
            android:onClick="scanclick" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Aici se vor afisa porturile deschise:"
            android:id="@+id/textView17"
            android:layout_gravity="center_horizontal" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="...va dura..."
            android:id="@+id/textView18"
            android:layout_gravity="center_horizontal" />
    </LinearLayout>
</RelativeLayout>

剂量&#34;可用&#34;实际上按照我的意图工作,或者我只是需要以其他方式进行工作?

0 个答案:

没有答案