代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
CSharp
】
多线程端口扫描程序
作者:
Dezai.CN
/ 发布于
2012/12/6
/
497
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(); }
试试其它关键字
端口扫描
同语言下
.
文件IO 操作类库
.
Check图片类型[JPEG(.jpg 、.jpeg),TIF,GIF,BMP,PNG,P
.
机器名和IP取得(IPV4 IPV6)
.
Tiff转换Bitmap
.
linqHelper
.
MadieHelper.cs
.
RegHelper.cs
.
如果关闭一个窗体后激活另一个窗体的事件或方法
.
创建日志通用类
.
串口辅助开发类
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
Dezai.CN
贡献的其它代码
(
4037
)
.
多线程Socket服务器模块
.
生成随机密码
.
清除浮动样式
.
弹出窗口居中
.
抓取url的函数
.
使用base HTTP验证
.
div模拟iframe嵌入效果
.
通过header转向的方法
.
Session操作类
.
执行sqlite输入插入操作后获得自动编号的ID
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3