ScanNetworkActivity

 avatar
hacker98
plain_text
5 months ago
5.8 kB
1
Indexable
main
package com.example.chatappdemo;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import java.io.IOException;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.concurrent.CountDownLatch;
public class ScanNetworkActivity extends AppCompatActivity {
    private static final String TAG = "NetworkScan";
    private static final int TIMEOUT_MS = 100; // Thời gian chờ (timeout) khi ping
    private ListView listView;
    private ArrayAdapter<String> adapter;
    private List<String> ipList = new ArrayList<>();
    private final int TOTAL_THREADS = 10;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scan_network);

        listView = findViewById(R.id.listView);
        adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, ipList);
        listView.setAdapter(adapter);

        ipList.clear();
        adapter.notifyDataSetChanged();
        CountDownLatch latch = new CountDownLatch(TOTAL_THREADS);
        Toast.makeText(ScanNetworkActivity.this, "Bắt đầu quét mạng...", Toast.LENGTH_SHORT).show();
        String subnet="192.168.244";
        new Thread(() -> startNetworkScan(subnet, 1, 25, latch)).start();
        new Thread(() -> startNetworkScan(subnet, 26, 50, latch)).start();
        new Thread(() -> startNetworkScan(subnet, 51, 75, latch)).start();
        new Thread(() -> startNetworkScan(subnet, 76, 100, latch)).start();
        new Thread(() -> startNetworkScan(subnet, 101, 125, latch)).start();
        new Thread(() -> startNetworkScan(subnet, 126, 150, latch)).start();
        new Thread(() -> startNetworkScan(subnet, 151, 175, latch)).start();
        new Thread(() -> startNetworkScan(subnet, 176, 200, latch)).start();
        new Thread(() -> startNetworkScan(subnet, 201, 225, latch)).start();
        new Thread(() -> startNetworkScan(subnet, 226, 254, latch)).start();
        // Tạo một luồng để đợi cho tất cả luồng quét hoàn thành
        new Thread(() -> {
            try {
                latch.await();
                runOnUiThread(() -> {
                    Toast.makeText(ScanNetworkActivity.this, "Quét mạng hoàn tất!", Toast.LENGTH_SHORT).show();
                });

            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
        // Xử lý khi người dùng chọn IP từ danh sách
        listView.setOnItemClickListener((parent, view, position, id) -> {
            String selectedIp = ipList.get(position);
            Toast.makeText(ScanNetworkActivity.this, "Đã chọn IP: " + selectedIp, Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(ScanNetworkActivity.this, ChatActivity.class);
            intent.putExtra("conversation_IP",selectedIp);
            startActivity(intent);
        });
    }

    private String getDeviceIpAddress() {
        try {
            for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) {
                NetworkInterface intf = en.nextElement();
                for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
                    InetAddress inetAddress = enumIpAddr.nextElement();
                    if (!inetAddress.isLoopbackAddress() && inetAddress.getHostAddress().matches("\\d+\\.\\d+\\.\\d+\\.\\d+")) {
                        return inetAddress.getHostAddress();
                    }
                }
            }
        } catch (Exception ex) {
            Log.e(TAG, "Không thể lấy địa chỉ IP của thiết bị", ex);
        }
        return null;
    }

    // Phương thức quét mạng cho một phạm vi IP nhất định
    private void startNetworkScan(String subnet, int start, int end, CountDownLatch latch) {
        String deviceIpAddress = getDeviceIpAddress();

        // Quét từng IP trong phạm vi start đến end
        for (int i = start; i <= end; i++) {
            String host = subnet + "." + i;

            // Bỏ qua IP của thiết bị hiện tại
            if (host.equals(deviceIpAddress)) {
                Log.d(TAG, "Bỏ qua địa chỉ IP của thiết bị: " + host);
                continue;
            }

            // Thực hiện quét từng IP và cập nhật giao diện
            try {
                InetAddress inetAddress = InetAddress.getByName(host);
                if (inetAddress.isReachable(TIMEOUT_MS)) {
                    Log.d(TAG, "Đã tìm thấy thiết bị: " + host);

                    // Cập nhật danh sách IP trên UI
                    runOnUiThread(() -> {
                        ipList.add(host);
                        adapter.notifyDataSetChanged();
                    });
                } else {
                    Log.d(TAG, "Không thể ping IP: " + host);
                }

                // Tạm dừng giữa các lần quét để giảm tải hệ thống
                Thread.sleep(100);  // Nghỉ 100ms giữa các lần quét để tránh quá tải

            } catch (IOException | InterruptedException e) {
                Log.e(TAG, "Lỗi khi quét IP: " + host, e);
            }
        }
        latch.countDown();
    }
}
Editor is loading...
Leave a Comment