Android Socketを使ってデバイスの接続を検索

今日の荘川桜です。一凛だけ季節外れの開花となりました。その枝の葉っぱも芽から少し成長しただけなので、この端末の枝だけが季節外れのようです。
ここしばらく姿を見なかったヌートリアがいました。害獣ですがほっとする安心感が感じられます。
亀これも外来種だと思われますが数匹と一緒に日向ぼっこしています。
この前のハトがいました。前と同じで葉っぱをかき分けるように歩いていました。柴犬が隣にいましたがその距離1mまで接近しました。
概要
Android のタブレットからローカルネットの接続を検索できるのか試してみました。
結構簡単なコードでIPアドレスとポート番号で検索できますが、タイムアウトの時間とソケットの作成と close で時間がかかりました。
2024年2月26日現在
WEBのみでは断片的で覚えにくいので最初に購入した Kotlin の本です。
実行結果
こんな感じで検索しています。
これが1件1秒なので終了まで延々と時間が過ぎます。

これの結果を得るのに、ポート番号80, 161, 443, 631, 5353, 9100の6通りあり、IPアドレスは192.168.0.0から192.138.0.49までの50通りの6×50の300通り、一つのタイムアウトが1秒なので5分以上かけています。
左が Asynctask を使ったコード、右が UiThread/WorkerThreadを使ったコード の実行で、全く同じ結果です。こんな結果ですがSocket の検索は結構時間がかかります。
サードパーティが開発する製品はこんな時間がかからずに結果がでており、どんな方法を使っているのか知りたいですね。

AsyncTaskを使ったコード
非同期の AsyncTask を使いました。
package org.sibainu.relax.room.myapplication;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.ArrayList;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ClickListener cl = new ClickListener();
Button btn;
btn = findViewById(R.id.button);
btn.setOnClickListener(cl);
}
// ----------
private class ClickListener implements View.OnClickListener {
@Override
public void onClick(View view) {
Log.d("Thread","開始");
startScan();
}
}
// ----------
private void startScan() {
try {
TextView tv;
tv = findViewById(R.id.textView);
// タスクの実行
new NetworkStateTask() {
@Override
protected void onPostExecute(String response) {
if (response == "1") {
Log.d("NetworkStateTask","success");
}
}
// 引数で TextView を渡してみました。
}.execute(tv);
} catch (Exception e){
Log.d("Exception", "err: " + e);
}
}
// ----------
private class NetworkStateTask extends AsyncTask<TextView, String, String> {
@Override
protected String doInBackground(TextView... params) {
// プリンタで使われる一般的なポート番号を要素にしました。
int[] portlist = {80, 161, 443, 631, 5353, 9100};
// int[] portlist = {80};
ArrayList<String> sl = new ArrayList<>();
final int timeOut = 1000;
for (int port : portlist) {
// Try to create the Socket on the given port.
try {
for (int subnet2 = 0; subnet2 < 1; subnet2++) {
for (int subnet = 0; subnet < 50; subnet++) {
// open a tcp socket
String server = String.format("192.168.%d.%d", subnet2, subnet);
Socket socket = new Socket();
try {
socket.connect(new InetSocketAddress(server, port), timeOut);
System.out.println("Network state of " + server + " port:" + port + " == " + socket.isConnected());
if (socket.isConnected()) {
sl.add(server + ":" + port);
}
}
catch (Exception e) {
System.out.println("Network state of " + server + " port:" + port + " == " + e);
}
finally {
try {
socket.close();
}
catch (Exception e) {
Log.d("Exception", "err close: " + e);
}
}
}
}
}
catch (Exception e) {
Log.d("Exception","err: " + e);
}
}
String response = "";
if (sl.size() > 0) {
response = String.join("\n",sl);
}
params[0].setText(response);
return "1";
}
}
}
AsyncTask は引数の関係がややこしいです。
execute(tv) から考えた方が私には分かりやすいです。tv は TextView なので AsyncTask<?,?,?> のところは最初に TextView がきて Asynctask<TextView,?,?> となります。
doInBackground() の引数指定は可変長引数なので 初めの代表を一つ(この場合 TextView )を使ってdoInBackground(TextView…params) となります。
AsyncTask<?,?,?> の3つ目は onPostExecute() の引数の型を指定します。この場合は String ですので AsyncTask<TextView, ?, String> となります。
AsyncTask<?,?,?> の2つ目は onProgressUpdate() の引数の型を指定します。この場合、String にしましたが、使う場合は Integer にします。

UiThread/WorkerThreadを使ったコード
package org.sibainu.relax.room.myapplication;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.annotation.UiThread;
import androidx.annotation.WorkerThread;
import androidx.appcompat.app.AppCompatActivity;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.ArrayList;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class MainActivity_async extends AppCompatActivity {
TextView GW;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ClickListener cl = new ClickListener();
Button btn;
btn = findViewById(R.id.button);
btn.setOnClickListener(cl);
}
private class ClickListener implements View.OnClickListener {
@Override
public void onClick(View view) {
Log.d("Thread", "開始");
TextView tv;
tv = findViewById(R.id.textView);
new PrinterScan(tv).startScan();
}
}
private class PrinterScan {
TextView _tv;
NetworkStateTask _backgroundReceiver;
public PrinterScan (TextView tv){
this._tv = tv;
}
@UiThread
public void startScan() {
try {
this._backgroundReceiver = new NetworkStateTask(this._tv);
ExecutorService executorService = Executors.newSingleThreadExecutor();
// タスクの実行
Future<String> future;
future = executorService.submit(this._backgroundReceiver);
String result = future.get();
if (result == "1") {
Log.d("result","success");
}
} catch (Exception e){
Log.d("Exception", "err: " + e);
}
}
public class NetworkStateTask implements Callable<String> {
TextView _tv;
public NetworkStateTask(TextView tv) {
this._tv = tv;
}
@WorkerThread
@Override
public String call() {
int[] portlist = {80, 161, 443, 631, 5353, 9100};
// int[] portlist = {80};
ArrayList<String> sl = new ArrayList<>();
final int timeOut = 1000;
for (int port : portlist) {
// Try to create the Socket on the given port.
try {
for (int subnet2 = 0; subnet2 < 1; subnet2++) {
for (int subnet = 0; subnet < 50; subnet++) {
// open a tcp socket
String server = String.format("192.168.%d.%d", subnet2, subnet);
Socket socket = new Socket();
try {
socket.connect(new InetSocketAddress(server, port), timeOut);
System.out.println("Network state of " + server + " port:" + port + " == " + socket.isConnected());
if (socket.isConnected()) {
sl.add(server + ":" + port);
}
}
catch (Exception e) {
System.out.println("Network state of " + server + " port:" + port + " == " + e);
}
finally {
try {
socket.close();
}
catch (Exception e) {
Log.d("Exception", "err close: " + e);
}
}
}
}
}
catch (Exception e) {
Log.d("Exception","err: " + e);
}
}
String response = "";
if (sl.size() > 0) {
response = String.join("\n",sl);
}
_tv.setText(response);
return "1";
}
}
}
}
Callable<>、Future<>、return の型を一致するようにしなければなりません。
この場合は、String ですが Integer、Map、List、Array などでも大丈夫でした。
このことを忘れるので記録します。

AndroidManifest.xml
インターネットの接続が必要ですのでパーミッションの追加が必要で、次のパーミッションを追加します。
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.MyApplication"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
res/layout/activity_main.xml
Button と TextView がそれぞれ一つの簡単なものです。
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:text="Button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView"
android:layout_width="200dp"
android:layout_height="0dp"
android:layout_marginTop="16dp"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button"
app:layout_constraintVertical_bias="0.5" />
</androidx.constraintlayout.widget.ConstraintLayout>
ここまでとします。