Menu

3/9/19

Xây dựng ứng dụng checkport.



Ưng dụng này dùng để kiểm tra tên miền Dyndns camera hay checkport IP wan, rất hữu ích.






















Design giao diện cho chương trình.



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/ip_add"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:hint="ex: 113.161.162.145"
        android:inputType="textPersonName"
        tools:ignore="Autofill" />

    <EditText
        android:id="@+id/port"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:hint="ex: 8080"
        android:inputType="textPersonName"
        tools:ignore="Autofill" />

    <Button
        android:id="@+id/btn_check"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:text="Check"
        android:textAllCaps="false" />

    <TextView
        android:id="@+id/txt_kq"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="25dp" />

</LinearLayout>

Sourcecode java:


package com.example.checkport;
import android.os.StrictMode;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
//thu vien socket, exception
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.SocketTimeoutException;

public class MainActivity extends AppCompatActivity {


    private Button btn_check;
    private EditText ip_add,port;
    private TextView txtkq;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //policy cho phep network tren thread chinh
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);

        //anh xa
        ip_add = (EditText) findViewById(R.id.ip_add);
        port = (EditText) findViewById(R.id.port);
        btn_check = (Button) findViewById(R.id.btn_check);
        txtkq = (TextView) findViewById(R.id.txt_kq);

        //su kien click cho button
        btn_check.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
             String S_ip =ip_add.getText().toString();
             int i_port=Integer.parseInt(port.getText().toString());
             mess (isSocketAliveUitlity(S_ip,i_port));
         }
     });

    }

    public boolean isSocketAliveUitlity(String hostName, int port) {
        boolean isAlive = false;

        // Creates a socket address from a hostname and a port number
        SocketAddress socketAddress = new InetSocketAddress(hostName, port);
        Socket socket = new Socket();
        // Timeout required - it's in milliseconds
        int timeout = 2000;
        //log("hostName: " + hostName + ", port: " + port);
        try {
            socket.connect(socketAddress, timeout);
            socket.close();
            isAlive = true;

        } catch (SocketTimeoutException exception) {
            //System.out.println(
               //     "SocketTimeoutException " + hostName + ":" + port + ". " + exception.getMessage());
        } catch (IOException exception) {
           // System.out.println(
                    //"IOException - Unable to connect to " + hostName + ":" + port + ". " + exception.getMessage());
        }

        return isAlive;
    }


    // Simple log utility returns boolean result
    private void mess (boolean isAlive)
    {


        if(!isAlive==false)
        {
            String txtopen = "<font color=#000000>Port is </font> <font color=#08670E>Open</font>";
            txtkq.setText(Html.fromHtml(txtopen));
        }
        else
        {
            String txtclose = "<font color=#000000>Port is </font> <font color=#BF1502>Close</font>";
            txtkq.setText(Html.fromHtml(txtclose));
        }


    }

}


No comments:

Post a Comment