AndroidStudio多线程端口扫描

农村四月闲人少,勤学苦攻把名扬。这篇文章主要讲述AndroidStudio多线程端口扫描相关的知识,希望能为你提供帮助。
android Studio 3.1.4
Build #AI-173.4907809, built on July 24, 2018
JRE: 1.8.0_152-release-1024-b02 amd64
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
Windows 10 10.0
【AndroidStudio多线程端口扫描】 
  Lucky~
 
1.XML布局界面文件

AndroidStudio多线程端口扫描

文章图片

1 < ?xml version="1.0" encoding="utf-8"?> 2 < android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 3xmlns:app="http://schemas.android.com/apk/res-auto" 4xmlns:tools="http://schemas.android.com/tools" 5android:layout_width="match_parent" 6android:layout_height="match_parent" 7tools:context=".PortScan"> 8 9< TextView 10android:id="@+id/textView3" 11android:layout_width="75dp" 12android:layout_height="26dp" 13android:layout_marginBottom="8dp" 14android:layout_marginEnd="8dp" 15android:layout_marginStart="8dp" 16android:layout_marginTop="8dp" 17android:text="PortMin" 18android:textColor="#FF00FF" 19android:textSize="18sp" 20app:layout_constraintBottom_toBottomOf="parent" 21app:layout_constraintEnd_toEndOf="parent" 22app:layout_constraintHorizontal_bias="0.0" 23app:layout_constraintStart_toStartOf="parent" 24app:layout_constraintTop_toTopOf="parent" 25app:layout_constraintVertical_bias="0.25" /> 26 27< TextView 28android:id="@+id/textView4" 29android:layout_width="75dp" 30android:layout_height="26dp" 31android:layout_marginBottom="8dp" 32android:layout_marginEnd="8dp" 33android:layout_marginStart="8dp" 34android:layout_marginTop="8dp" 35android:text="PortMax" 36android:textColor="#FF00FF" 37android:textSize="18sp" 38app:layout_constraintBottom_toBottomOf="parent" 39app:layout_constraintEnd_toEndOf="parent" 40app:layout_constraintHorizontal_bias="0.65" 41app:layout_constraintStart_toStartOf="parent" 42app:layout_constraintTop_toTopOf="parent" 43app:layout_constraintVertical_bias="0.25" /> 44 45< TextView 46android:id="@+id/textView" 47android:layout_width="137dp" 48android:layout_height="42dp" 49android:layout_marginBottom="8dp" 50android:layout_marginEnd="8dp" 51android:layout_marginStart="8dp" 52android:layout_marginTop="8dp" 53android:text="PortScan" 54android:textColor="#FF00FF" 55android:textSize="30sp" 56app:layout_constraintBottom_toBottomOf="parent" 57app:layout_constraintEnd_toEndOf="parent" 58app:layout_constraintHorizontal_bias="0.05" 59app:layout_constraintStart_toStartOf="parent" 60app:layout_constraintTop_toTopOf="parent" 61app:layout_constraintVertical_bias="0.00999999" /> 62 63< TextView 64android:id="@+id/textView2" 65android:layout_width="75dp" 66android:layout_height="26dp" 67android:layout_marginBottom="8dp" 68android:layout_marginEnd="8dp" 69android:layout_marginStart="8dp" 70android:layout_marginTop="8dp" 71android:text="IP" 72android:textColor="#FF00FF" 73android:textSize="18sp" 74app:layout_constraintBottom_toBottomOf="parent" 75app:layout_constraintEnd_toEndOf="parent" 76app:layout_constraintHorizontal_bias="0.0" 77app:layout_constraintStart_toStartOf="parent" 78app:layout_constraintTop_toTopOf="parent" 79app:layout_constraintVertical_bias="0.13999999" /> 80 81< EditText 82android:id="@+id/ipedit" 83android:layout_width="267dp" 84android:layout_height="42dp" 85android:layout_marginBottom="8dp" 86android:layout_marginEnd="8dp" 87android:layout_marginStart="8dp" 88android:layout_marginTop="8dp" 89android:ems="10" 90android:inputType="textPersonName" 91android:text="127.0.0.1" 92android:textSize="18sp" 93app:layout_constraintBottom_toBottomOf="parent" 94app:layout_constraintEnd_toEndOf="parent" 95app:layout_constraintHorizontal_bias="1.0" 96app:layout_constraintStart_toStartOf="parent" 97app:layout_constraintTop_toTopOf="parent" 98app:layout_constraintVertical_bias="0.13" /> 99 100< EditText 101android:id="@+id/portmin" 102android:layout_width="80dp" 103android:layout_height="42dp" 104android:layout_marginBottom="8dp" 105android:layout_marginEnd="8dp" 106android:layout_marginStart="8dp" 107android:layout_marginTop="8dp" 108android:ems="10" 109android:inputType="textPersonName" 110android:text="1" 111app:layout_constraintBottom_toBottomOf="parent" 112app:layout_constraintEnd_toEndOf="parent" 113app:layout_constraintHorizontal_bias="0.35" 114app:layout_constraintStart_toStartOf="parent" 115app:layout_constraintTop_toTopOf="parent" 116app:layout_constraintVertical_bias="0.239" /> 117 118< EditText 119android:id="@+id/portmax" 120android:layout_width="80dp" 121android:layout_height="42dp" 122android:layout_marginBottom="8dp" 123android:layout_marginEnd="8dp" 124android:layout_marginStart="8dp" 125android:layout_marginTop="8dp" 126android:ems="10" 127android:inputType="textPersonName" 128android:text="65535" 129app:layout_constraintBottom_toBottomOf="parent" 130app:layout_constraintEnd_toEndOf="parent" 131app:layout_constraintHorizontal_bias="1.0" 132app:layout_constraintStart_toStartOf="parent" 133app:layout_constraintTop_toTopOf="parent" 134app:layout_constraintVertical_bias="0.239" /> 135 136< Button 137android:id="@+id/button" 138android:layout_width="369dp" 139android:layout_height="42dp" 140android:layout_marginBottom="8dp" 141android:layout_marginEnd="8dp" 142android:layout_marginStart="8dp" 143android:layout_marginTop="180dp" 144android:onClick="play" 145android:text="play" 146android:textColor="#FF00FF" 147app:layout_constraintBottom_toBottomOf="parent" 148app:layout_constraintEnd_toEndOf="parent" 149app:layout_constraintStart_toStartOf="parent" 150app:layout_constraintTop_toTopOf="parent" 151app:layout_constraintVertical_bias="0.005" /> 152 153< TextView 154android:id="@+id/tv" 155android:layout_width="368dp" 156android:layout_height="23dp" 157android:layout_marginBottom="16dp" 158android:layout_marginEnd="8dp" 159android:layout_marginStart="8dp" 160android:layout_marginTop="8dp" 161android:text="ing..." 162android:textColor="#FF00FF" 163android:textSize="14sp" 164app:layout_constraintBottom_toBottomOf="parent" 165app:layout_constraintEnd_toEndOf="parent" 166app:layout_constraintStart_toStartOf="parent" 167app:layout_constraintTop_toBottomOf="@+id/button" 168app:layout_constraintVertical_bias="1.0" /> 169 170< ListView 171android:id="@+id/listview" 172android:layout_width="378dp" 173android:layout_height="287dp" 174android:layout_marginBottom="8dp" 175android:layout_marginEnd="8dp" 176android:layout_marginStart="16dp" 177android:layout_marginTop="8dp" 178app:layout_constraintBottom_toTopOf="@+id/tv" 179app:layout_constraintEnd_toEndOf="parent" 180app:layout_constraintStart_toStartOf="parent" 181app:layout_constraintTop_toBottomOf="@+id/button" 182app:layout_constraintVertical_bias="1.0" /> 183 184 < /android.support.constraint.ConstraintLayout>

2.添加权限
< ?xml version="1.0" encoding="utf-8"?> < manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.shawna.portscan"> < uses-permission android:name="android.permission.INTERNET" /> < uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> < application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> < activity android:name=".PortScan"> < intent-filter> < action android:name="android.intent.action.MAIN" /> < category android:name="android.intent.category.LAUNCHER" /> < /intent-filter> < /activity> < /application> < /manifest>

3.项目代码

1 package com.shawna.portscan; 2 3 import android.os.Handler; 4 import android.os.Message; 5 import android.support.v7.app.AlertDialog; 6 import android.support.v7.app.AppCompatActivity; 7 import android.os.Bundle; 8 import android.view.View; 9 import android.widget.ArrayAdapter; 10 import android.widget.EditText; 11 import android.widget.ListView; 12 import android.widget.TextView; 13 14 import java.net.InetSocketAddress; 15 import java.net.Socket; 16 import java.net.SocketAddress; 17 import java.util.concurrent.ExecutorService; 18 import java.util.concurrent.Executors; 19 20 public class PortScan extends AppCompatActivity { 21 22int portmin = 1; //最小ip取值 23int portmax = 65535; //最大ip取值 24String ip; //ip地址 25String[] ports = new String[0]; //成功连接的端口保存数组 26String portstext; //成功连接的端口文本 27 28@Override 29protected void onCreate(Bundle savedInstanceState) { 30super.onCreate(savedInstanceState); 31setContentView(R.layout.activity_port_scan); 32 33//状态条显示初始化信息 34thistext("处理器核心数 " + thisGetCPUnum() + " 核,将使用 " + thisGetCPUnum()*2 + " 线程执行..."); 35} 36 37public void play(View view){ 38//获取各个编辑框组件的内容 39EditText ipEdit = (EditText) findViewById(R.id.ipedit); 40EditText piedit = (EditText) findViewById(R.id.portmin); 41EditText paedit = (EditText) findViewById(R.id.portmax); 42ip = ipEdit.getText().toString(); 43portmin = Integer.parseInt(piedit.getText().toString()); 44portmax = Integer.parseInt(paedit.getText().toString()); 45 46//判断输入的内容是否合法 47if (iftext() == false){ 48AlertDialog.Builder message = new AlertDialog.Builder(this); 49message.setMessage("有数据漏填,请检查!"); 50AlertDialog m1 = message.create(); 51m1.show(); 52return; 53} 54 55//启动线程执行任务 56thisiflink thisiflink = new thisiflink(); 57thisiflink.start(); 58} 59 60//获取线程传出的消息更新UI 61private Handler.Callback mCallback = new Handler.Callback() { 62@Override 63public boolean handleMessage(Message msg) { 64if (msg.what == 1) { 65if (msg.obj == "更新UI列表"){ 66if (portstext == null || portstext.length() == 0) { 67return false; 68} else { 69//以#为分隔符分割文本 70ports = portstext.split("\\\\#"); 71//调用函数更新UI状态条 72thislist(ports); 73} 74} 75}else if (msg.what == 1011){ 76if (msg.obj == null || msg.obj.toString().length() == 0){ 77return false; 78}else { 79//调用函数更新UI状态条 80thistext(msg.obj.toString()); 81} 82} 83 84return false; 85} 86}; 87 88//创建一个Handler用于线程通讯 89Handler mHandler = new Handler(mCallback); 90 91//获取CPU核心数 92public int thisGetCPUnum(){ 93return(Runtime.getRuntime().availableProcessors()); 94} 95 96//列表框刷新数据 97public void thislist(String[] text){ 98ListView mListView; 99mListView = (ListView) findViewById(R.id.listview); 100ArrayAdapter< String> adapter = new ArrayAdapter< String> (this,android.R.layout.simple_list_item_1,text); 101mListView.setAdapter(adapter); 102} 103 104//增加数据到数组 105public static String[] thisArrayAdd(String[] text,String data){ 106//调用自定义函数增加数组成员 107text = thisLengthArray(text); 108//对新增加的数组成员赋值 109text[text.length] = data; 110//输出这个新数组 111return text; 112} 113 114//状态条显示文本 115public void thistext(String text){ 116TextView textView = (TextView) findViewById(R.id.tv); 117textView.setText(text); 118} 119 120//数组增加成员数 121public static String[] thisLengthArray(String[] text){ 122String[] newArray = new String[text.length+1]; 123System.arraycopy(text,0,newArray,0,text.length); 124return newArray; 125} 126 127class thisiflink extends Thread{ 128//新建一个固定线程的线程池,线程数量为CPU核心数量的2倍 129ExecutorService pool = Executors.newFixedThreadPool(thisGetCPUnum()*2); 130@Override 131public void run(){ 132//取任务开始时的时间 133final long start = System.currentTimeMillis(); 134//防止数据重复执行前清空已保存的数据 135portstext = null; 136//循环执行,一个端口占用一个线程 137for(int i = portmin ; i < = portmax ; i++) { 138final int port = i; 139Runnable runnable = new Runnable() { 140@Override 141public void run() { 142boolean x = true; 143try { 144//port被5整除,则刷新UI状态条,显示当前扫描的端口 145if (port%5 == 0){ 146Message msg1 = Message.obtain(); 147msg1.what = 1011; 148msg1.arg1 = 1; 149msg1.obj = "正在扫描 " + port + " 端口,请稍候..."; 150mHandler.sendMessage(msg1); 151} 152 153//socket尝试与指定IP的端口进行连接,超时300毫秒 154Socket mSocket; 155mSocket = new Socket(); 156SocketAddress socketAddress = new InetSocketAddress(ip, port); 157mSocket.connect(socketAddress, 300); 158mSocket.close(); 159 160//如果成功连接则输出连接成功 161System.out.println("端口 " + port + " 连接成功!"); 162 163//防止下面代码有异常抛出将x赋值为false 164x = false; 165 166//将数据保存到porttext中 167if (portstext == null || portstext.length() == 0){ 168portstext = "端口 " + port + " 连接成功!"; 169}else { 170portstext = portstext + "#端口 " + port + " 连接成功!"; 171} 172 173//通知刷新UI列表 174Message msg = Message.obtain(); 175msg.what = 1; 176msg.arg1 = 1; 177msg.obj = "更新UI列表"; 178mHandler.sendMessage(msg); 179} catch (Exception e) { 180

    推荐阅读