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
相关文档:
先贴代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Ref_and_Out_test
{
class Program
{
static void Main(string[] args)
& ......
//获得汉字的区位码
byte[] array = new byte[2];
array = System.Text.Encoding.Default.GetBytes("啊");
int i1 = (short)(array[0] - ''\0'');
int i2 = (short)(array[1] - ''\0'');
//unicode解码方式下的汉字码
array = System.Text.Encoding.Unicode.GetBytes("啊");
i1 = (short)(arra ......
为什么要使用C#泛型?
为了了解这个问题,我们先看下面的代码,代码省略了一些内容,但功能是实现一个栈,这个栈只能处理int数据类型:
public class Stack
{
private int[] m_item;
public ......
变量、常量及表达式变量和常量变量(静态、非静态、数组元素、值参数、引用参数、输出参数、局部变量)静态(static) 如 public static int x; 一旦静态变量所属的类被装载,直到包含该类的程序运行结束时它一直存在。非静态:不带有static修饰符声明的变量称为实例变量,如int a ;常量(attributes constnt-modifiers CO ......