一个端口扫描的小程序

  今天有个朋友让我帮他做个扫描端口的小程序,就是看某台机器打开了哪些端口,既然已经写了就顺便贴出来一下,虽然不是什么复杂东西,给相关需要的人参考一下吧:)下面是原代码
  1 None.gif using  System;
  2 None.gif using  System.Collections.Generic;
  3 None.gif using  System.ComponentModel;
  4 None.gif using  System.Data;
  5 None.gif using  System.Drawing;
  6 None.gif using  System.Text;
  7 None.gif using  System.Windows.Forms;
  8 None.gif using  System.Threading;
  9 None.gif using  System.Net;
 10 None.gif using  System.Net.Sockets;
 11 None.gif
 12 None.gif namespace  Scan
 13 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 14InBlock.gif    public partial class Main : Form
 15ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 16InBlock.gif        //主线程
 17InBlock.gif        private Thread tdm;
 18InBlock.gif        //开始端口,结束端口
 19InBlock.gif        int Start, End;
 20InBlock.gif       //线程数
 21InBlock.gif        private int count=0;
 22InBlock.gif        //当前扫描端口
 23InBlock.gif        private int i;
 24InBlock.gif        public Main()
 25ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 26InBlock.gif            InitializeComponent();
 27ExpandedSubBlockEnd.gif        }

 28ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 29InBlock.gif        /// 副线程
 30ExpandedSubBlockEnd.gif        /// </summary>

 31InBlock.gif     private void Process()
 32ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 33InBlock.gif         //当前要扫描的IP地址
 34InBlock.gif            string IP;
 35InBlock.gif        
 36InBlock.gif            IP = mkIP.Text;
 37InBlock.gif
 38InBlock.gif                TcpClient tc = new TcpClient();
 39InBlock.gif                tc.SendTimeout = tc.ReceiveTimeout = 20000;
 40InBlock.gif
 41InBlock.gif                try
 42ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 43InBlock.gif                    //建立连接
 44InBlock.gif                    tc.Connect(IP, i);
 45InBlock.gif                    //判断是否连接成功
 46InBlock.gif                    if (tc.Connected)
 47ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
 48InBlock.gif                        tbDetail.Text = tbDetail.Text + "端口" + i.ToString() + "开放。\r\n";
 49ExpandedSubBlockEnd.gif                    }

 50InBlock.gif                   
 51ExpandedSubBlockEnd.gif                }

 52InBlock.gif                catch (Exception ex)
 53ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 54InBlock.gif                   
 55ExpandedSubBlockEnd.gif                }

 56InBlock.gif                finally
 57ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 58InBlock.gif  tc.Close();
 59InBlock.gif                    tc = null;
 60InBlock.gif                    //修改线程数
 61InBlock.gif                    count--;
 62InBlock.gif//写入进度  
 63InBlock.gif pbProcess.Value = Convert.ToInt32((Convert.ToDouble(i) / Convert.ToDouble(End)) * 100);
 64ExpandedSubBlockEnd.gif                }

 65InBlock.gif               
 66InBlock.gif            
 67InBlock.gif           
 68ExpandedSubBlockEnd.gif        }

 69InBlock.gif
 70InBlock.gif        private void button1_Click(object sender, EventArgs e)
 71ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 72InBlock.gif            Thread tmain = new Thread(new ThreadStart(MainProcess));
 73InBlock.gif            tmain.Start();
 74InBlock.gif           
 75ExpandedSubBlockEnd.gif        }

 76InBlock.gif        //主线程
 77InBlock.gif        private void MainProcess()
 78ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 79InBlock.gif            count = 0;
 80InBlock.gif            pbProcess.Value = 0;
 81InBlock.gif            tbDetail.Text = "";
 82InBlock.gif Start = Convert.ToInt32(tbStar.Text);
 83InBlock.gif            End = Convert.ToInt32(tbEnd.Text);
 84InBlock.gif            //循环取副线程扫描端口
 85InBlock.gif            for (i = Start; i < End; i++)
 86ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 87InBlock.gif                count++;
 88InBlock.gif               
 89InBlock.gif                tdm = new Thread(new ThreadStart(Process));
 90InBlock.gif                tdm.Start();
 91InBlock.gif                Thread.Sleep(10);
 92InBlock.gif                //线程数控制在20
 93InBlock.gif                while (count > 20) ;
 94ExpandedSubBlockEnd.gif            }

 95InBlock.gif           while (count>0) ;
 96InBlock.gif            tbDetail.Text = tbDetail.Text+"扫描完成";
 97ExpandedSubBlockEnd.gif        }

 98InBlock.gif        private void Main_Load(object sender, EventArgs e)
 99ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
100InBlock.gif            Control.CheckForIllegalCrossThreadCalls = false;
101ExpandedSubBlockEnd.gif        }

102InBlock.gif
103InBlock.gif       
104ExpandedSubBlockEnd.gif    }

105InBlock.gif   
106ExpandedBlockEnd.gif}

转载于:https://www.cnblogs.com/DarkAngel/archive/2006/11/29/576273.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值