自己写的端口扫描程序

在开始研究编程之前,我想当一名黑客,很羡慕那些牛人写的那些端口扫描工具,比如x-scan,我当时就想,这种厉害的程序是怎么写出来的呢?后来开始研究编程了,先是用的vb,vb开始有了一定的研究以后,就写了一个端口扫描工具,但是占用的系统资源超大,每次运行的时候cpu使用率都是100,很恐怖,也变相的否定了我的程序,现在一直研究c#,虽然没有达到精通的程度,但是也可以自己写一些自己需要的工具了,于是又想写一个端口扫描工具,而且也真的写了出来。虽然说现在的端口扫描工具已经有很多了,而且好的也不少,但是自己写就是想圆自己原来的梦想,就写了这么一个。每次看到这个程序,就会想起原来的那些日子,呵呵 原来已经走过来了,但是仍然是无法忘记的。这个程序的开发环境是.net2005,也就是.net2.0,程序有点简陋。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace WindowsApplication5
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            AboutBox1 myabout = new AboutBox1();
            myabout.Show();

        }

        private void textBox4_TextChanged(object sender, EventArgs e)
        {

        }
        IPAddress ipRemote;
        int startport;
        int endport;
        int a;
        int b;
        int nowport;
        TcpClient tc;
      
        string myhost;
        Thread startscan;
        bool localscan = false;
       

        //TimeSpan d1 = DateTime.Now.TimeOfDay;


        private void scan()
        {
            textBox1.ReadOnly = true;
            button1.Enabled = false;
            int progmax = b - a + 1;   //设置进度条的范围
            int pvalue = 0;
            this.progress1.Maximum = progmax;

            this.progress1.Value = pvalue;
            for (nowport = a; nowport <=endport; nowport++)
            {
                tc = new TcpClient();

                tc.SendTimeout = tc.ReceiveTimeout = 2000;
                //nowport = a;
                try
                {
                    //tc = new TcpClient();

                    //tc.SendTimeout = tc.ReceiveTimeout = 1000;


                    if (checkBox1.Checked)
                    {
                        tc.Connect(myhost, nowport);
                    }
                    else
                    {
                        tc.Connect(ipRemote, nowport);
                    }
                    if (tc.Connected)
                    {


                        this.listBox1.Items.Add(nowport.ToString() + " is open");
                        //label4.Text = nowport.ToString();
                        listBox1.Refresh();

                    }

                }
                catch
                {
                    //容错处理


                    this.listBox1.Items.Add(nowport.ToString() + " is closed");
                    //label4.Text = nowport.ToString();
                    listBox1.Refresh();

                }
                finally
                {
                    tc.Close();
                    tc = null;
                    label4.Text = nowport.ToString();
                    pvalue++;
                    progress1.Value = pvalue;
                    //Thread.Sleep(10);
                }
               


            }
           

 

            textBox1.ReadOnly = false;
            if (checkBox1.Checked)
            {
                localscan = true;
            }
            else
            {
                localscan = false;
            }
            checkBox1.Enabled = true;
            button1.Enabled = true;
            this.label4.Text = "";

            startscan.Abort();
           
         
        }
        private void button1_Click(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
            {
                textBox1.Text = textBox1.Text;
                textBox4.Text = "Now scan:" + textBox1.Text;
                checkBox1.Enabled = false;
            }
            else
            {
                try
                {

                    ipRemote = IPAddress.Parse(textBox1.Text);

                }

                catch //判断给定的IP地址的合法性
                {

                    MessageBox.Show("输入的IP地址不合法!", "错误!");

                    return;

                }
            }
            try//判断端口是否合法
            {
                startport = int.Parse(textBox2.Text);
                endport = int.Parse(textBox3.Text);
            }
            catch
            {
                MessageBox.Show("请输入合法的数字","错误!");
                return;
            }
            if (startport < 1 | startport > 65536 | endport<1 | endport >65535)
            {
                MessageBox.Show("windows的端口在1-65535之间","错误!");
            }
            if (endport < startport)
            {
                a = endport;
                b = startport;

            }
            else
            {
                a = startport;
                b = endport;
            }
            //创建扫描线程
            startscan = new Thread(new ThreadStart(scan));
            startscan.Start();

            }

        private void Form1_Load(object sender, EventArgs e)
        {
          
           
                this.textBox1.Text = "192.168.2.5";
           
           
            //this.button2.Enabled = false;
          
        }

        private void button4_Click(object sender, EventArgs e)
        {
          
            listBox1.Items.Clear();
            textBox4.Clear();
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (this.checkBox1.Checked)
            {
                myhost = Dns.GetHostName();
                textBox1.Text = myhost;
                textBox1.ReadOnly = true;
              
            }
            else {
                textBox1.ReadOnly = false;
                textBox1.Text = "";
            }
           
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                startscan.Abort();
                textBox1.ReadOnly = false;
                if (checkBox1.Checked)
                {
                    localscan = true;
                }
                else
                {
                    localscan = false;
                }
                this.label4.Text = "";
                checkBox1.Enabled = true;
                button1.Enabled = true;

            }
            catch
            {
                MessageBox.Show("扫描线程还没有启动","错误!");
            }
          
          

        }

        private void groupBox2_Enter(object sender, EventArgs e)
        {

        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }
    }
}

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值