批量检测指定ip某些端口是否开启,用来做什么自己想吧!!!

技术文摘
0 254
admin
xukai 5 2021/1/20 20:30:37
编辑此贴 收藏

描述:-


程序运行界面截图:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace CheckIpPORT
{
    public partial class FrmMain : Form
    {
        public FrmMain()
        {
            InitializeComponent();
        }

        private string netaddress;
        private int endAdd;
        //结束地址
        public int EndAdd
        {
            get { return endAdd; }
            set { endAdd = value; }
        }
        private int startAdd;
        int port;
        /// <summary>
        /// 端口
        /// </summary>
        public int Port
        {
            get { return port; }
            set { port = value; }
        }
        /// <summary>
        /// 开始地址
        /// </summary>
        public int StartAdd
        {
            get { return startAdd; }
            set { startAdd = value; }
        }
        //网段
        public string Netaddress
        {
            get { return netaddress; }
            set { netaddress = value; }
        }

        ErrorProvider err = new ErrorProvider();
        bool CheckInput()
        {
            err.Clear();
            IPAddress ip;
            if (!IPAddress.TryParse(textBox_ip.Text, out ip))
            {
                err.SetError(textBox_ip, "ip不合法!");
                return false;
            }
            int v = 0;
            if (!int.TryParse(textBox_start.Text, out v))
            {
                err.SetError(textBox_start, "格式有误");
                return false;
            }
            startAdd = v;
            if (!int.TryParse(textBox_end.Text, out v))
            {
                err.SetError(textBox_end, "格式有误");
                return false;
            }
            endAdd = v;
            if (endAdd < startAdd)
            {
                err.SetError(textBox_end, "终止ip不能大于起始ip");
                return false;
            }
            if (!int.TryParse(textBox_port.Text, out v))
            {
                err.SetError(textBox_port, "端口不合法");
                return false;
            }
            else
            {
                if (v <= 0 || v >= 65535)
                {
                    err.SetError(textBox_port, "端口不合法");
                    return false;
                }
            }
            port = v;
            return true;
        }
        Thread th;
        private void button1_Click(object sender, EventArgs e)
        {
            if (CheckInput())
            {
                string ipCheck = textBox_ip.Text.Substring(0, textBox_ip.Text.LastIndexOf('.'));
                netaddress = ipCheck;
                this.button1.Enabled = false;
                this.button1.Text = "正在检测,请稍后";
                this.button2.Enabled = true;
                th = new Thread(new ThreadStart(CheckStatus));
                th.IsBackground = true;
                th.Start();

                //MessageBox.Show(ipCheck);
            }
        }

        void SyncTimeOut(Socket client)
        {
            int i = 2, k = 0;
            Thread th2 = new Thread(new ThreadStart(() =>
            {
                while (true)
                {
                    Thread.Sleep(1000);
                    k++;
                    if (k >= i)
                    {
                        try { client.Close(); }
                        catch { }
                    }
                }
            }));
            th2.IsBackground = true;
            th2.Start();
        }

        void CheckStatus()
        {
            bool b = false;
            string ips = string.Empty;
            for (int i = startAdd; i <= endAdd; i++)
            {
                ips = netaddress + "." + i;
                IPAddress ip = IPAddress.Parse(ips);
                try
                {
                    IPEndPoint point = new IPEndPoint(ip, port);
                    Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                    SyncTimeOut(sock);
                    sock.Connect(point);
                    b = true;
                    //Console.WriteLine("连接端口{0}成功!", point);
                }
                catch (SocketException e)
                {
                    if (e.ErrorCode != 10061)
                    {
                        Console.WriteLine(e.Message);
                    }
                    //Console.WriteLine("连接{0}失败", i);
                    b = false;
                }
                //listView1.Items.Add(new ListViewItem(new string[] { ips, port.ToString(), b ? "开启" : "未开启" }));
                listView1.Invoke(new delBandData(bindData), ips, b);
            }
            this.button1.Enabled = true;
            this.button1.Text = "开始检测";
        }

        public delegate void delBandData(string ip, bool b);

        void bindData(string ips, bool b)
        {
            listView1.Items.Add(new ListViewItem(new string[] { ips, port.ToString(), b ? "开启" : "未开启" }));
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.Disposed += Form1_Disposed;
        }

        void Form1_Disposed(object sender, EventArgs e)
        {
            try
            {
                th.Abort();
            }
            catch (Exception)
            {

            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                button1.Enabled = true;
                button1.Text = "开始检测";
                th.Abort();
            }
            catch (Exception ex)
            {

            }
        }
    }

    class IP_PORT_STATUS
    {
        string ip;
        int port;
        bool status;

        public bool Status
        {
            get { return status; }
            set { status = value; }
        }

        public int Port
        {
            get { return port; }
            set { port = value; }
        }

        public string Ip
        {
            get { return ip; }
            set { ip = value; }
        }
    }
}


个人签名:己所不欲勿施于人

回帖
  • 消灭零回复
本周热议
非法访问资源ip地址公布 1
不用写一句sql的ORM框架,支持多种数据库 1
c# winform 身份证信息读取 0
c#如何集成控制Led屏,实现数据发送自动开机、关机等操作! 0
c#如何获取电脑mac地址,超简单! 0
批量检测指定ip某些端口是否开启,用来做什么自己想吧!!! 0
c# 实现文件下载功能,带下载进度条,可用到软件的自动更新中 0
winform程序如何做到开机自动启动 0
怎么判断两张图片是否完全相同,通过读取图片内容进行对比 0
c# 如何打印条形码 0
热门资讯
看图猜成语:明明很简单,是不是大意了 65 
【每日一习话】把事业放在心上 把责任扛在肩上 64 
第一观察 | 从两会“如期”召开看百年大党的执政能力 63 
美日共同发表"对中国措辞最强硬"声明 外交部撂重话 61 
玖月奇迹王小玮近照曝光!穿紧身裤秀出小鸟腿,皮肤白到发光 60 
成人体验馆被封,客人一天点3个娃娃:男人的孤独,谁能懂? 59 
《奇葩说》第七季总决赛,肖骁用不是自己风格的夺冠后,获得表扬 59 
联播+ | 谋定而动 习近平引领京津冀携手下好“一盘棋” 58 
强力堆料,几乎没有短板、铭瑄iCraft B550M电竞之心主板评测 58 
美媒承认搞错了,但已经来不及了 56 
广东一男子离职索赔270万,公司:你月薪才25万 55 
王霜首次回应结束留洋原因:不因贾秀全 只因这3个字 54 
扒女明星每日穿搭,千元Coach这样搭,千元CHANEL这样买! 53 
亲戚之子前来投奔杜月笙,故意在家中掉钱,捡与不捡两个结局 29 
多名主播带货遭网友围攻 耐克小姐姐被骂哭 阿迪小哥要去找工作 18