- [DEVEXPRESS] Hướng dẫn bật tính năng Scroll Pixcel in Touch trên GridView
- [DEVEXPRESS] Hướng dẫn sử dụng TileBar viết ứng dụng duyệt hình ảnh Winform
- [DEVEXPRESS] Tô màu border TextEdit trên Winform
- [C#] Lấy dữ liệu từ Console Write hiển thị lên textbox Winform
- [C#] Hiển thị Progress bar trên Window Console
- [C#] Di chuyển control Runtime và lưu layout trên winform
- [SQLSERVER] Sử dụng hàm NULL IF
- [C#] Chia sẽ source code mã đi tuần bằng giao diện Winform
- [C#] Flash Window in Taskbar Winform
- Download và Giải nén tập tin File sử dụng Powershell
- [C#] Hướng dẫn cách lấy thông tin đăng nhập tài khoản và mật khẩu web browser trên windows
- [VB.NET] CRUD Thêm xóa sửa tìm kiếm Realtime FireBase
- [C#] Hiển thị thông báo Toast Message trong lập trình Winform
- [C#] Cấu hình định dạng ngày tháng, thời gian trên Windows cho ứng dụng Winform
- [C#] Rút gọn đường dẫn link url với TinyURL API
- [C#] Hướng dẫn cách bo tròn winform with Radius
- [C#] Chia sẽ class BackGroundOverlay Show Modal cho Winform
- [C#] Hướng dẫn Flip Image Winform
- [C#] Invoke là gì? cách sử dụng phương thức Invoke()
- [C#] Hướng dẫn chia sẽ file, folder từ ứng dụng sang Zalo Chat
[C#] Hướng dẫn ping port sử dụng app Paping của google
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:
Ở 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#:
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!