NEWS

[C#] Hướng dẫn ping port sử dụng app Paping của google

[C#] Hướng dẫn ping port sử dụng app Paping của google
Đăng bởi: Thảo Meo - Lượt xem: 7652 08:20:59, 26/08/2020PHẦN MỀM

Xin chào các bạn bài viết hôm nay mình sẽ hướng dẫn các bạn cách kiểm tra ping một port đặc biệt nào đó có mở hay không sử dụng Paping app của google tích hợp vào ứng dụng Winform C#.

[C#] Ping Special port in Winform

Vậy Paping là gì?

Paping (pronounced pah ping) là một công cụ giúp cho quản trị viên dễ dàng kiểm tra kết nối của một port nào đó và thời gian trả về kết quả ping.

Các bạn có thể download paping từ code của google hoặc download ở cuối bài viết của mình nhé.

Dưới đây là giao diện của tool Paping:

paping_csharp

Ở hình trên các bạn thấy app đang ping đến địa chỉ ip 192.168.0.194 và kiểm tra port 554

và kết quả dưới là thời gian trả về kết quả ở cột time.

Và bài viết này mình sẽ tích hợp tool này vào app C# để kiểm tra một cách dễ dàng.

Để sử dụng paping chỉ cần gõ:

Cú pháp: paping [địa chỉ ip] -p [Port]

Dưới đây là giao diện tích hợp ping paping vào app c#:

ping_demo

Source code paping C#:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PingTool
{
    public partial class Form1 : Form
    {
        Process process;
        [DllImport("user32.dll")]
        static extern int SetWindowText(IntPtr hWnd, string text);
        public Form1()
        {
            InitializeComponent();
        }

        private void btn_ping_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txt_ipaddress.Text.Trim()))
            {
                MessageBox.Show("Bạn chưa nhập địa chỉ IP/Domain!", "Thông báo!");
                return;
            }

            if (string.IsNullOrEmpty(txt_port.Text.Trim()))
            {
                MessageBox.Show("Bạn chưa nhập Port!", "Thông báo!");
                return;
            }
            runCMD();
        }

        public void runCMD()
        {
            Properties.Settings.Default.ipaddress = txt_ipaddress.Text.Trim();
            Properties.Settings.Default.port = txt_port.Text.Trim();
            Properties.Settings.Default.Save();
            try
            {
                if(process != null)
                {
                    process.CancelErrorRead();
                    process.CancelOutputRead();
                    process.Kill();
                }
              
            }
            catch (Exception)
            {

            }
            rtbResult.Clear();
            string command = $@"{Application.StartupPath}paping.exe {txt_ipaddress.Text} -p {txt_port.Text}";
            process = new Process();
            process.StartInfo.FileName = "cmd.exe";
            if (!chkMultiWindows.Checked)
            {
                process.StartInfo.CreateNoWindow = true;
            }          
            process.StartInfo.Arguments = "/c " + command;
            process.StartInfo.UseShellExecute = false;
            if (!chkMultiWindows.Checked)
            {
                process.StartInfo.RedirectStandardOutput = true;
                process.StartInfo.RedirectStandardError = true;
                process.OutputDataReceived += new DataReceivedEventHandler(OutputHandler);
                process.ErrorDataReceived += new DataReceivedEventHandler(OutputHandler);
            }
           
           

            process.Start();        

            if (!chkMultiWindows.Checked)
            {
                process.BeginOutputReadLine();
                process.BeginErrorReadLine();
            }
            



        }

        private void OutputHandler(object sender, DataReceivedEventArgs e)
        {
            
            rtbResult.BeginInvoke(new Action(() => {
                var data = e.Data + Environment.NewLine;
                if (data.Contains("timed out"))
                {
                    rtbResult.AppendText(data, Color.Red);
                }
                else
                {
                    rtbResult.AppendText(data);
                }
               
                rtbResult.ScrollToCaret();
            }));
        }

        private void btn_stop_Click(object sender, EventArgs e)
        {
            if (process != null)
            {
              
                try
                {
                    process.CancelErrorRead();
                    process.CancelOutputRead();
                    process.Kill();
                }
                catch (Exception)
                {
                 
                }
             
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            txt_ipaddress.Text = Properties.Settings.Default.ipaddress;
            txt_port.Text = Properties.Settings.Default.port;
            chkMultiWindows.Checked = Properties.Settings.Default.multiwindows;
        }

        private void txt_ipaddress_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.KeyCode == Keys.Enter)
            {
                runCMD();
            }
        }

        private void txt_port_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                runCMD();
            }
        }

        private void chkMultiWindows_CheckedChanged(object sender, EventArgs e)
        {
            Properties.Settings.Default.multiwindows = chkMultiWindows.Checked;
            Properties.Settings.Default.Save();
        }
    }
}

Thanks for watching!

DOWNLOAD SOURCE

 

Tags: ping console c#ping port c#

THÔNG TIN TÁC GIẢ

BÀI VIẾT LIÊN QUAN

[C#] Hướng dẫn ping port sử dụng app Paping của google
Đăng bởi: Thảo Meo - Lượt xem: 7652 08:20:59, 26/08/2020PHẦN MỀM