首页 | 互联网 | IT动态 | IT培训 | Cisco | Windows | Linux | Java | .Net | Oracle | 软件测试 | C/C++ | 嵌入式开发 | 存储世界 | 服务器
网络设备 | IDC | 安全 | 求职招聘 | 数字网校 | 网页设计 | 平面设计 | 技术专题 | 电子书下载 | 教学视频 | 源码下载 | 搜索 | 博客 | 论坛
首页 | JAVA | C# | VB | VB.NET | C/C++ | delphi | 工程管理 | 其他语言 | 论坛
各大城市软件开发培训、软件人才免费咨询热线:400-700-5807
 您现在的位置: 中国IT实验室 >> 桌面开发 >> C# >> 正文
C#编写的多线程端口扫描程序
来源:ChinaItLab 作者:佚名 时间:2007-4-7

  有幸购买到Visual studio .net 的光盘,急忙安装一套,一用感觉非常好,所学写端口扫描程序一只,献给本版的朋友们:)
 using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
//增加的如下..
using System.Data;
using System.Net.Sockets;
using System.Net;
using System.IO;
using System.Text;
using System.Threading;

namespace iPortScan
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
//该处放的是程序要用到的公共变量
public string scanHost = Dns.GetHostName(); //默认当前本机IP
public Int32 tport = 0; //当前连接端口编号
public Int32 connState = 0; //扫描状态
public int portSum = 0 ; //端口总计
public bool endThread = false; //结束状态
public AutoResetEvent asyncOpsAreDone = new AutoResetEvent(false);
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox txtHostname;
private System.Windows.Forms.Button cmdExec;
private System.Windows.Forms.ListBox logList;
public System.Windows.Forms.CheckedListBox portList;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.NumericUpDown sNum;
private System.Windows.Forms.NumericUpDown eNum;
private System.Windows.Forms.CheckBox showdie;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.ToolTip toolTip1;
private System.Windows.Forms.StatusBar statusBar1;
private System.Windows.Forms.LinkLabel linkLabel1;
private System.Windows.Forms.LinkLabel linkLabel2;
private System.ComponentModel.IContainer components;
/*
此处省略了各个调用的控件的属性设置代码
*/
static void Main()
{
Application.Run(new Form1());
}

private void cmdExec_Click(object sender, System.EventArgs e)
{
Int32 startPort = (Int32)sNum.Value;
Int32 endPort = (Int32)eNum.Value;
if(txtHostname.Text.Length==0)
{
MessageBox.Show("请输入一个主机的名称吧!","系统提示");
txtHostname.Text = scanHost.ToString();
txtHostname.Focus();
return;
}
if(startPort>endPort)
{
MessageBox.Show("错误,起始端口必须要小于结束的端口!","系统提示");
startPort = endPort-1;
sNum.Text = startPort.ToString();
sNum.Focus();
return ;
}

if(cmdExec.Text=="&Scan")
{
endThread= false;
cmdExec.Text= "&Stop";
}
else
{
endThread= true;
cmdExec.Text= "&Scan";
}

if(endThread!=true)
{
connState = 0;
portSum = 0;
scanHost = txtHostname.Text;
try
{
IPAddress ipaddr =(IPAddress)Dns.Resolve(scanHost).AddressList.GetValue(0);
txtHostname.Text = ipaddr.ToString();
}
catch
{
txtHostname.Focus();
MessageBox.Show("请输入正确的主机地址,该地址DNS无法解析","系统提示");
return ;
}
logList.Items.Clear();

for (Int32 threadNum = startPort; threadNum <=endPort; threadNum++)
{
ThreadPool.QueueUserWorkItem(new WaitCallback(Startscan),threadNum);
logList.Items.Add ("扫描端口:" + threadNum.ToString());
}

}
}
public void Startscan(Object state)
{
Int32 port = (Int32) state;
string tMsg = "";
string getData = "";
int lindex = 0;
int eindex = 0;
connState++; //判断线程数目
if(endThread==true)
{
if(connState==((Int32)eNum.Value-(Int32)sNum.Value))
{
cmdExec.Text = "&Scan";
logList.Items.Add ("扫描完毕!");
}
else
{
cmdExec.Text = "&Stop";
logList.Items.Add ("正在停止对"+port.ToString()+"端口的扫描线程");
}
logList.Items.Add("结束线程:"+port.ToString());
asyncOpsAreDone.Close();
}
else
{
try
{
TcpClient tcp = new TcpClient();
tcp.Connect(scanHost,port);
//该处如果建立连接错误的话,将不执行下面的代码..
portSum ++;
lindex = portList.Items.Add(port.ToString() + "端口开放",false);
portList.SelectedIndex=lindex;
Stream sm = tcp.GetStream();
sm.Write(Encoding.Default.GetBytes(tMsg.ToCharArray()),0,tMsg.Length);
StreamReader sr = new StreamReader(tcp.GetStream(),Encoding.Default);
getData = sr.ReadLine();
if(lindex!=0&&getData.Length!=0)
{
tMsg = " +-" + port.ToString() + "端口数据:"+getData.ToString();
eindex = portList.Items.Add(tMsg); //插入一条信息记录
portList.Items.Insert(lindex+1,tMsg);
portList.Items.RemoveAt(eindex);
}
sr.Close();
sm.Close();
tcp.Close();
}
catch
{
//显示坏死的端口
if(showdie.Checked==true)
{
portList.Items.Add(port.ToString()+"端口无法连接,回传数据为空");
}
}
finally
{
Thread.Sleep(0);
logList.Items.Add("结束线程:"+port.ToString());
asyncOpsAreDone.Close();
statusBar1.Text = "端口总计:"+portSum.ToString() ;
if(connState==((Int32)eNum.Value-(Int32)sNum.Value))
{
cmdExec.Text = "&Scan";
}
}
}
}

private void button1_Click(object sender, System.EventArgs e)
{
Application.Exit();
}

总结:基于.Net 框架开发程序确实简单方便,并且功能强大。
下载代码与执行文件:http://202.103.224.224/icools/bbs/non-cgi/usr/5_5.zip
【责编:Lili】
中国IT教育热线咨询
相关文章
几种VC++数据库开发技术的相对比较
利用C#实现标注式消息提示窗口
用C#创建COM对象
Visual C#多线程参数传递浅析…
用C#创建COM对象
正确理解C#中的ref关键字
利用C#实现标注式消息提示窗口
C#和Visual Basic中的闭包与对象生存时期…
C#的前途如何?
C#写的ADSL拨号程序示例
推荐文章

 精彩友情推荐
·Asp源码 PHP源码
·CGI源码 JSP源码
·建站书籍教程
·服务器软件 .net源码
·建站工具软件
·IDC资讯大全
·机房品质万里行
·IDC托管必备知识
·全国IDC报价
·网站推广优化
最新更新 推荐文章
·框架:J2EE WEB应用架构分析…03-13
·几种VC++数据库开发技术的相对比…03-13
·利用C#实现标注式消息提示窗口03-13
·用C#创建COM对象03-13
·Visual C#多线程参数传递浅析…03-13
·Visual C#多线程参数传递浅析…03-13
·基于HOOK和MMF的Win密码渗透技术11-15
·Visual C++设计超强仿QQ自动伸缩…11-15
·Java SE 6.0实现高质量桌面集成开…11-15
·史玉柱东山再起幕后高人11-15
·用C#创建COM对象09-06
·IT管理十大失误及其对策08-30
·VC中利用MFC设计绘图程序初步08-23
·JAVA中对象创建和初始化过程08-23
·C语言中的位域的使用08-09
·浅谈Java桌面应用程序开发08-09
·C#的前途如何?08-02
·几种VC++数据库开发技术的相对比较07-12
·用Visual C#实现网络封包监视…07-12
·VB.NET中的TextBox控件详解07-12
·VB.NET实现PC与掌上电脑PPC的双向通信07-05