• 一个小工具。

  • namespace IPPort_CheckTool
    {
    partial class MainForm
    {
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.IContainer components = null; /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
    protected override void Dispose(bool disposing)
    {
    if (disposing && (components != null))
    {
    components.Dispose();
    }
    base.Dispose(disposing);
    } #region Windows 窗体设计器生成的代码 /// <summary>
    /// 设计器支持所需的方法 - 不要修改
    /// 使用代码编辑器修改此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
    this.groupBox1 = new System.Windows.Forms.GroupBox();
    this.txt_Time = new System.Windows.Forms.Label();
    this.txt_CoreNum = new System.Windows.Forms.Label();
    this.txt_Active = new System.Windows.Forms.Label();
    this.txt_NoActive = new System.Windows.Forms.Label();
    this.btn_Start = new System.Windows.Forms.Button();
    this.label3 = new System.Windows.Forms.Label();
    this.label2 = new System.Windows.Forms.Label();
    this.label1 = new System.Windows.Forms.Label();
    this.txt_PortEnd = new System.Windows.Forms.TextBox();
    this.txt_PortStart = new System.Windows.Forms.TextBox();
    this.txt_IP = new System.Windows.Forms.TextBox();
    this.listBox_NoActive = new System.Windows.Forms.ListBox();
    this.listBox_ActivePort = new System.Windows.Forms.ListBox();
    this.progressBar = new System.Windows.Forms.ProgressBar();
    this.groupBox1.SuspendLayout();
    this.SuspendLayout();
    //
    // groupBox1
    //
    this.groupBox1.Controls.Add(this.progressBar);
    this.groupBox1.Controls.Add(this.txt_Time);
    this.groupBox1.Controls.Add(this.txt_CoreNum);
    this.groupBox1.Controls.Add(this.txt_Active);
    this.groupBox1.Controls.Add(this.txt_NoActive);
    this.groupBox1.Controls.Add(this.btn_Start);
    this.groupBox1.Controls.Add(this.label3);
    this.groupBox1.Controls.Add(this.label2);
    this.groupBox1.Controls.Add(this.label1);
    this.groupBox1.Controls.Add(this.txt_PortEnd);
    this.groupBox1.Controls.Add(this.txt_PortStart);
    this.groupBox1.Controls.Add(this.txt_IP);
    this.groupBox1.Dock = System.Windows.Forms.DockStyle.Top;
    this.groupBox1.Location = new System.Drawing.Point(3, 3);
    this.groupBox1.Margin = new System.Windows.Forms.Padding(5);
    this.groupBox1.Name = "groupBox1";
    this.groupBox1.Padding = new System.Windows.Forms.Padding(5);
    this.groupBox1.Size = new System.Drawing.Size(1058, 127);
    this.groupBox1.TabIndex = 0;
    this.groupBox1.TabStop = false;
    this.groupBox1.Text = "配置:";
    //
    // txt_Time
    //
    this.txt_Time.AutoSize = true;
    this.txt_Time.ForeColor = System.Drawing.Color.Green;
    this.txt_Time.Location = new System.Drawing.Point(844, 101);
    this.txt_Time.Name = "txt_Time";
    this.txt_Time.Size = new System.Drawing.Size(74, 21);
    this.txt_Time.TabIndex = 10;
    this.txt_Time.Text = "耗时:0s";
    //
    // txt_CoreNum
    //
    this.txt_CoreNum.AutoSize = true;
    this.txt_CoreNum.ForeColor = System.Drawing.Color.Green;
    this.txt_CoreNum.Location = new System.Drawing.Point(610, 101);
    this.txt_CoreNum.Name = "txt_CoreNum";
    this.txt_CoreNum.Size = new System.Drawing.Size(120, 21);
    this.txt_CoreNum.TabIndex = 9;
    this.txt_CoreNum.Text = "CPU核心总数:0";
    //
    // txt_Active
    //
    this.txt_Active.AutoSize = true;
    this.txt_Active.ForeColor = System.Drawing.Color.Green;
    this.txt_Active.Location = new System.Drawing.Point(389, 101);
    this.txt_Active.Name = "txt_Active";
    this.txt_Active.Size = new System.Drawing.Size(119, 21);
    this.txt_Active.TabIndex = 8;
    this.txt_Active.Text = "激活端口总数:0";
    //
    // txt_NoActive
    //
    this.txt_NoActive.AutoSize = true;
    this.txt_NoActive.Location = new System.Drawing.Point(3, 101);
    this.txt_NoActive.Name = "txt_NoActive";
    this.txt_NoActive.Size = new System.Drawing.Size(135, 21);
    this.txt_NoActive.TabIndex = 7;
    this.txt_NoActive.Text = "未激活端口总数:0";
    //
    // btn_Start
    //
    this.btn_Start.Location = new System.Drawing.Point(915, 30);
    this.btn_Start.Name = "btn_Start";
    this.btn_Start.Size = new System.Drawing.Size(123, 56);
    this.btn_Start.TabIndex = 6;
    this.btn_Start.Text = "开始测试端口是否激活";
    this.btn_Start.UseVisualStyleBackColor = true;
    this.btn_Start.Click += new System.EventHandler(this.btn_Start_Click);
    //
    // label3
    //
    this.label3.AutoSize = true;
    this.label3.Location = new System.Drawing.Point(530, 41);
    this.label3.Name = "label3";
    this.label3.Size = new System.Drawing.Size(78, 21);
    this.label3.TabIndex = 5;
    this.label3.Text = "结束端口:";
    //
    // label2
    //
    this.label2.AutoSize = true;
    this.label2.Location = new System.Drawing.Point(252, 41);
    this.label2.Name = "label2";
    this.label2.Size = new System.Drawing.Size(78, 21);
    this.label2.TabIndex = 4;
    this.label2.Text = "开始端口:";
    //
    // label1
    //
    this.label1.AutoSize = true;
    this.label1.Location = new System.Drawing.Point(23, 41);
    this.label1.Name = "label1";
    this.label1.Size = new System.Drawing.Size(29, 21);
    this.label1.TabIndex = 3;
    this.label1.Text = "IP:";
    //
    // txt_PortEnd
    //
    this.txt_PortEnd.Location = new System.Drawing.Point(633, 41);
    this.txt_PortEnd.Name = "txt_PortEnd";
    this.txt_PortEnd.Size = new System.Drawing.Size(150, 29);
    this.txt_PortEnd.TabIndex = 2;
    //
    // txt_PortStart
    //
    this.txt_PortStart.Location = new System.Drawing.Point(355, 41);
    this.txt_PortStart.Name = "txt_PortStart";
    this.txt_PortStart.Size = new System.Drawing.Size(150, 29);
    this.txt_PortStart.TabIndex = 1;
    //
    // txt_IP
    //
    this.txt_IP.Location = new System.Drawing.Point(77, 41);
    this.txt_IP.Name = "txt_IP";
    this.txt_IP.Size = new System.Drawing.Size(150, 29);
    this.txt_IP.TabIndex = 0;
    //
    // listBox_NoActive
    //
    this.listBox_NoActive.Dock = System.Windows.Forms.DockStyle.Left;
    this.listBox_NoActive.ForeColor = System.Drawing.Color.Gray;
    this.listBox_NoActive.FormattingEnabled = true;
    this.listBox_NoActive.ItemHeight = 21;
    this.listBox_NoActive.Location = new System.Drawing.Point(3, 130);
    this.listBox_NoActive.Name = "listBox_NoActive";
    this.listBox_NoActive.Size = new System.Drawing.Size(383, 548);
    this.listBox_NoActive.TabIndex = 1;
    //
    // listBox_ActivePort
    //
    this.listBox_ActivePort.Dock = System.Windows.Forms.DockStyle.Fill;
    this.listBox_ActivePort.ForeColor = System.Drawing.Color.Green;
    this.listBox_ActivePort.FormattingEnabled = true;
    this.listBox_ActivePort.ItemHeight = 21;
    this.listBox_ActivePort.Location = new System.Drawing.Point(386, 130);
    this.listBox_ActivePort.Name = "listBox_ActivePort";
    this.listBox_ActivePort.Size = new System.Drawing.Size(675, 548);
    this.listBox_ActivePort.TabIndex = 2;
    //
    // progressBar
    //
    this.progressBar.Location = new System.Drawing.Point(798, 41);
    this.progressBar.Name = "progressBar";
    this.progressBar.Size = new System.Drawing.Size(100, 29);
    this.progressBar.TabIndex = 11;
    //
    // MainForm
    //
    this.AutoScaleDimensions = new System.Drawing.SizeF(10F, 21F);
    this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
    this.ClientSize = new System.Drawing.Size(1064, 681);
    this.Controls.Add(this.listBox_ActivePort);
    this.Controls.Add(this.listBox_NoActive);
    this.Controls.Add(this.groupBox1);
    this.Font = new System.Drawing.Font("微软雅黑", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
    this.Margin = new System.Windows.Forms.Padding(5);
    this.Name = "MainForm";
    this.Padding = new System.Windows.Forms.Padding(3);
    this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
    this.Text = "Hello!";
    this.groupBox1.ResumeLayout(false);
    this.groupBox1.PerformLayout();
    this.ResumeLayout(false); } #endregion private System.Windows.Forms.GroupBox groupBox1;
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.TextBox txt_PortEnd;
    private System.Windows.Forms.TextBox txt_PortStart;
    private System.Windows.Forms.TextBox txt_IP;
    private System.Windows.Forms.Label label2;
    private System.Windows.Forms.Label label3;
    private System.Windows.Forms.Button btn_Start;
    private System.Windows.Forms.ListBox listBox_NoActive;
    private System.Windows.Forms.ListBox listBox_ActivePort;
    private System.Windows.Forms.Label txt_NoActive;
    private System.Windows.Forms.Label txt_Active;
    private System.Windows.Forms.Label txt_CoreNum;
    private System.Windows.Forms.Label txt_Time;
    private System.Windows.Forms.ProgressBar progressBar;
    }
    }
    using System;
    using System.Configuration;
    using System.Linq;
    using System.Net;
    using System.Net.NetworkInformation;
    using System.Net.Sockets;
    using System.Threading.Tasks;
    using System.Windows.Forms; namespace IPPort_CheckTool
    {
    public partial class MainForm : Form
    {
    private Configuration config = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);
    private DateTime startTime;
    private Task[] tasks; private NotifyIcon notifyIcon; public MainForm()
    {
    InitializeComponent(); var now = DateTime.Now;
    this.Text = $"Hello 今天是{now.ToString("dddd")}哟 "; this.txt_IP.Text = ConfigurationManager.AppSettings["IP"].ToString();
    this.txt_PortStart.Text = ConfigurationManager.AppSettings["MinPortStart"].ToString();
    this.txt_PortEnd.Text = ConfigurationManager.AppSettings["MaxPortEnd"].ToString();
    this.txt_CoreNum.Text = $"CPU核心总数:{Environment.ProcessorCount}"; this.components = new System.ComponentModel.Container();
    this.notifyIcon = new NotifyIcon(this.components);
    this.notifyIcon.Visible = true;
    this.notifyIcon.MouseClick += (sn, en) => { if (en.Button == MouseButtons.Left) this.Show(); };
    var menus = new ContextMenu();
    var menu1= new MenuItem("退出软件");
    menu1.Click += (s1, e1) => { this.Close(); };
    menus.MenuItems.Add(menu1);
    notifyIcon.ContextMenu= menus;
    } private void btn_Start_Click(object sender, EventArgs e)
    {
    string ip = this.txt_IP.Text.Trim();
    string min = this.txt_PortStart.Text.Trim();
    string max = this.txt_PortEnd.Text.Trim(); if (string.IsNullOrWhiteSpace(ip) || string.IsNullOrWhiteSpace(min) || string.IsNullOrWhiteSpace(max))
    {
    MessageBox.Show("不能为空!");
    return;
    } if (!int.TryParse(min, out var minPort) || !int.TryParse(max, out var maxPort))
    {
    MessageBox.Show("端口不是整数!");
    return;
    } config.AppSettings.Settings["IP"].Value = ip;
    config.AppSettings.Settings["MinPortStart"].Value = min;
    config.AppSettings.Settings["MaxPortEnd"].Value = max;
    config.Save();
    ConfigurationManager.RefreshSection("appSettings"); IPAddress targetIpAddress = IPAddress.Parse(ip);
    var ipadrlist = Dns.GetHostAddresses(Dns.GetHostName()).ToList();
    ipadrlist.Add(IPAddress.Parse("127.0.0.1"));
    ipadrlist.Add(IPAddress.Parse("0.0.0.0"));
    if (ipadrlist.Contains(targetIpAddress))
    {
    CheckLocalPort(minPort, maxPort);//本地IP
    }
    else
    {
    CheckRemotePort(ip, minPort, maxPort);//远程IP
    }
    } private void CheckLocalPort(int minPort, int maxPort)
    {
    btn_Start.Enabled = false; listBox_NoActive.Items.Clear();
    listBox_ActivePort.Items.Clear();
    startTime = DateTime.Now;
    int length = maxPort - minPort + 1;
    progressBar.Maximum = length;
    progressBar.Minimum = 0; var ips = IPGlobalProperties.GetIPGlobalProperties();
    var activeIps = ips.GetActiveTcpListeners().Select(p => p.Port).ToHashSet(); Task.Factory.StartNew(new Action(() =>
    {
    for (int i = minPort; i <= maxPort; i++)
    {
    int port = i;
    this?.Invoke(new Action(() =>
    {
    if (activeIps.Contains(port))
    {
    listBox_ActivePort.Items.Insert(0, port);
    txt_Active.Text = $"激活端口总数:{listBox_ActivePort.Items.Count}";
    }
    else
    {
    listBox_NoActive.Items.Insert(0, port);
    txt_NoActive.Text = $"未激活端口总数:{listBox_NoActive.Items.Count}";
    }
    progressBar.Value = listBox_ActivePort.Items.Count + listBox_NoActive.Items.Count;
    txt_Time.Text = $"耗时:{(DateTime.Now - startTime).TotalSeconds.ToString()}s"; //耗时操作卡死UI 处理方法
    Application.DoEvents();
    }));
    } })).ContinueWith(p => InitUI()); } private void CheckRemotePort(string ip, int minPort, int maxPort)
    {
    btn_Start.Enabled = false; listBox_NoActive.Items.Clear();
    listBox_ActivePort.Items.Clear();
    startTime = DateTime.Now;
    int length = maxPort - minPort + 1;
    tasks = new Task[length];
    progressBar.Maximum = length;
    progressBar.Minimum = 0; Task.Run(() =>
    {
    for (int i = minPort; i <= maxPort; i++)
    {
    int port = i;
    var task = Task.Factory.StartNew(new Action(() =>
    {
    var res = CheckRemotePortCanConnect(ip, port);
    this?.Invoke(new Action(() =>
    {
    if (res.Item1)
    {
    listBox_ActivePort.Items.Insert(0, res.Item2);
    txt_Active.Text = $"激活端口总数:{listBox_ActivePort.Items.Count}";
    }
    else
    {
    listBox_NoActive.Items.Insert(0, res.Item2);
    txt_NoActive.Text = $"未激活端口总数:{listBox_NoActive.Items.Count}";
    }
    progressBar.Value = listBox_ActivePort.Items.Count + listBox_NoActive.Items.Count;
    txt_Time.Text = $"耗时:{(DateTime.Now - startTime).TotalSeconds.ToString()}s"; //耗时操作卡死UI 处理方法
    Application.DoEvents();
    }));
    }));
    tasks[i - minPort] = task;
    } Task.WaitAll(tasks); }).ContinueWith(p => InitUI());
    } private void InitUI()
    {
    this?.Invoke(new Action(() => { btn_Start.Enabled = true; }));
    } private (bool,int) CheckRemotePortCanConnect(string ipString, int port)
    {
    IPAddress ip = IPAddress.Parse(ipString); bool isConnect = false; TcpClient tcp = new TcpClient();
    try
    {
    tcp.Connect(ip, port);
    isConnect = tcp.Connected;
    }
    catch (Exception)
    {
    isConnect = false;
    }
    finally
    {
    tcp?.Close();
    tcp?.Dispose();
    }
    return (isConnect, port);
    }
    }
    }

C# WinForm 检查目标主机的端口是否可连接的更多相关文章

  1. ORA-12545: 因目标主机或对象不存在, 连接失败

    ORA-12545: 因目标主机或对象不存在, 连接失败 1. 问题描述 XP系统下同时安装了AX1应用程序和升级版AX2,连接同一个在本机Oracle客户端上配置的连接实例,其中AX2显示链接成功, ...

  2. 使用C#winform编写渗透测试工具--端口扫描

    使用C#winform编写渗透测试工具--端口扫描器 主要介绍使用C#winform编写渗透测试工具--端口扫描器,端口扫描器则是一种检测服务器或者主机虚拟端口是开启或关闭的工具.由于连接到局域网或互 ...

  3. 通过端口 8080 连接到主机 localhost 的 TCP/IP 连接失败

    错误:“connect timed out.请验证连接属性,并检查 SQL Server 的实例正在主机上运行,且在此端口接受 TCP/IP 连接,还要确保防火墙没有阻止到此端口的 TCP 连接.”这 ...

  4. 【转】Java JDBC连接SQL Server2005错误:通过端口 1433 连接到主机 localhost 的 TCP/IP 连接失败

    错误原因如下: Exception in thread "main" org.hibernate.exception.JDBCConnectionException: Cannot ...

  5. Java JDBC连接SQL Server2005错误:通过端口 1433 连接到主机 localhost 的 TCP/IP 连接失败 及sql2008外围服务器

    转载:Java JDBC连接SQL Server2005错误:通过端口 1433 连接到主机 localhost 的 TCP/IP 连接失败 错误原因如下: Exception in thread & ...

  6. 浅谈如何检查Linux中开放端口列表

    给大家分享一篇关于如何检查Linux中的开放端口列表的详细介绍,首先如果你想检查远程Linux系统上的端口是否打开请点击链接浏览.如果你想检查多个远程Linux系统上的端口是否打开请点击链接浏览.如果 ...

  7. 使用MSF发现主机和端口扫描

    使用MSF发现主机和端口扫描 使用search命令查找需要的模块 MSF模块太多,记不住怎么办!!! 我们不需要记住所有模块,我们只要能找到我们想用的模块就行,平时积累使用的模块也行哦! 比如,我们通 ...

  8. sql 2000 无法连接远程数据库 sqlserver不存在或访问被拒绝、不能打开到主机的连接,在端口1433:连接失败等 解决方案

    问题: sql 2000 无法连接远程数据库 sqlserver不存在或访问被拒绝 telnet 127.0.0.1 1433     提示:不能打开到主机的连接,在端口1433:连接失败 解决方案: ...

  9. SQLServerException:通过端口 1433 连接到主机 localhost 的 TCP/IP 连接失败。

    一.问题描述: 1.连接数据库时抛出的异常: com.microsoft.sqlserver.jdbc.SQLServerException: 通过端口 1433 连接到主机 localhost 的 ...

  10. Python_跟随目标主机IP变换

    ''' 为了防止黑客攻击或者负载均衡,会经常变换主机,这样同一个域名在不同时间可能会对应不同的IP地址,在这种情况下可以通过 socket模块的gethostbyname()函数来实时获取目标主机的I ...

随机推荐

  1. JS 通过后台接口返回的URL地址下载文件并保存到本地(已在项目中使用,保存音视频文件)

    今天做antdV表格勾选下载操作时,因为粗心大意碰到了下载问题,特此记录~ 一.单个文件下载逻辑代码如下: const exportFile = (data, fileName, _this)=> ...

  2. 掌控物体运动艺术:图扑 Easing 函数实践应用

    现如今,前端开发除了构建功能性的网站和应用程序外,还需要创建具有吸引力且尤为流畅交互的用户界面,其中动画技术在其中发挥着至关重要的作用.在数字孪生领域,动画的应用显得尤为重要.数字孪生技术通过精确模拟 ...

  3. CMake 生成器表达式---条件表达式和逻辑运算符

    [写在前面] CMake 的生成器表达式用于在构建系统级别上进行条件判断和逻辑运算,它们通常用在目标属性和生成器表达式上下文中.这些表达式允许你根据不同的平台.配置或编译器来定制构建过程. 本文引用的 ...

  4. uniswap v2 类比

    当然,以下是生活中的类比,帮助你理解 Uniswap V2 的核心概念: 1. 自动化做市商(AMM) 生活中的例子:自动售货机 解释:自动售货机内部有一种商品(如饮料)和一定的库存.用户通过投币购买 ...

  5. openresty操作mongodb

    最近项目中使用openresty,需要通过openresty连接mongo,经过几番折腾终于有了一个结果,现将其记录下来,也感谢模块提供者 使用openresty操作mongo 1.引入第三方的模块 ...

  6. .NET 全功能流媒体管理控制接口平台

    前言 视频会议.在线教育.直播娱乐还是远程监控,流媒体平台的性能和稳定性直接影响着用户体验. 给大家推荐一个基于 C# 开发的全功能流媒体管理控制接口平台. 项目介绍 AKStream是一个基于 C# ...

  7. 经典C语言题目——打印罗汉塔图形

    打印如下图形: ++++1 +++22 ++333 +4444 55555 点击查看代码 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> ...

  8. Java net.sf.jxls 生成模板 并导出excel

    如果是 maven项目需要引入下面这个就可以 <dependency> <groupId>net.sf.jxls</groupId> <artifactId& ...

  9. PA1-总结

    前言 代码全是自己写的,没看过参考代码,思路也有部分和指导书不一样,算是个原创?然后毕竟pa1是简单的部分,也没有什么值得骄傲的地方,只是作为一次记录. 毕竟自己的水平还是有限,可能部分地方会有些bu ...

  10. Python之常见问题

    一.问题出现:使用Pycharm设置虚拟环境后,打开终端出现如下报错:无法加载文件-\venv\Scripts\activate.ps1,因为在此系统上禁止运行脚本. 解决方式已管理员的身份打开pow ...