C#扫描计算机端口
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace PortScanner
{
class Program
{
//已扫描端口数目
internal static int scannedCount = 0;
//正在运行的线程数目
internal static int runningThreadCount = 0;
//打开的端口数目
internal static List<int> openedPorts = new List<int>();
//起始扫描端口
static int startPort = 1;
//结束端口号
static int endPort = 500;
//最大工作线程数
static int maxThread = 10;
static void Main(string[] args)
{
//接收传入参数一作为要扫描的主机
string host = "192.168.0.1";
//接收传入参数二作为端口扫描范围,如1-4000
string portRange = "1-400";
startPort = int.Parse(portRange.Split('-')[0].Trim());
endPort = int.Parse(portRange.Split('-')[1].Trim());
for (int port = startPort; port < endPort; port++)
{
//创建扫描类
Scanner scanner = new Scanner(host, port);
Thread thread = new Thread(new ThreadStart(scanner.Scan));
thread.Name = port.ToString();
thread.IsBackground = true;
//启动扫描线程
thread.Start();
runningThreadCount++;
Thread.Sleep(10);
//循环,直到某个线程工作完毕才启动另一新线程,也可以叫做推拉窗技术
while (runningThreadCount >= maxThread) ;
}
//空循环,直到所有端口扫描完毕
while (scannedCount + 1 < (endPort - startPort)) ;
Console.WriteLine();
Console.WriteLine();
//输出结果
Console.WriteLine("Scan for host: {0} has been completed , \n total {1} ports scanned, \nopened ports :{2}",
host, (endPort - startPort), openedPorts.Count);
foreach (int port in openedPorts)
Console.WriteLine("\tPort: {0} is open", port.ToString().PadLeft(6));
}
}
//扫描类
class Scanner
{
string m_host;
int m_port;
public Scanner(string host, int port)
{
m_h
相关文档:
public class yzzSerialize
{
private yzzSerialize()
{ }
private static yzzCache cache = new yzzCache();
public static T GetfromXml<T>(string xmlpath, T t)
{
using (FileStream fs = new FileStream(xmlpath, FileMode.Open, FileAcces ......
/// <summary>
/// 支持XML序列化的泛型 Dictionary
/// </summary>
/// <typeparam name="TKey"></typeparam>
/// <typeparam name="TValue"></typeparam>
[XmlRoot("SerializableDictionary")]
public class SerializableDictionary<TKey, TValue& ......
这本书讲解
C#
语言十分详细,我将其中的重要内容整理成条款,以备忘。
1.
事件是在满足某个特定条件时发生的,触发
(raise)
事件的对象称为发布者
(publisher)
,对这个事件进行响应的对象称为订阅者
(subscriber)
。事件处理程序是注册到事件的方法,可在任何类或者结构 ......
变量、常量及表达式变量和常量变量(静态、非静态、数组元素、值参数、引用参数、输出参数、局部变量)静态(static) 如 public static int x; 一旦静态变量所属的类被装载,直到包含该类的程序运行结束时它一直存在。非静态:不带有static修饰符声明的变量称为实例变量,如int a ;常量(attributes constnt-modifiers CO ......