C#进行端口扫描的源码
下面代码内容是关于C#进行端口扫描的的代码,应该是对码农们有一些好处。
网站建设哪家好,找创新互联公司!专注于网页设计、网站建设、微信开发、小程序制作、集团企业网站建设等服务项目。为回馈新老客户创新互联还提供了青龙免费建站欢迎大家使用!
using System;
using System.Net;
using System.Net.Sockets;
using System.Collections;
namespace PortScan
{
class Program
{
static void Main(string[] args)
{
IPAddress ip;
int startPort, endPort;
{
Console.ReadKey();
}
}
#region 从命令行参数 中提取端口 + static bool GetPortRange(string[] args, out int startPort, out int endPort)
private static bool GetPortRange(string[] args,out IPAddress ip, out int startPort, out int endPort)
{
ip = null;
startPort = endPort = 0;
if (args.Length != 0 && (args[0] == "/?" || args[0] == "/help"))
{
Console.WriteLine("Scan port from startPort to endPort of the host specified by the IPAddress.");
Console.WriteLine("Command Format:");
Console.WriteLine("PortScan IPAddress startPort endPort");
Console.WriteLine("For example:");
Console.WriteLine("PortScan 127.0.0.1 1 1024");
return false;
}
if (args.Length == 3)
{
if (IPAddress.TryParse(args[0],out ip) && int.TryParse(args[1], out startPort) && int.TryParse(args[2], out endPort))
{
return true;
}
else
{
Console.WriteLine("参数格式不正确!");
return false;
}
}
else
{
Console.WriteLine("参数数目不正确!");
return false;
}
}
#endregion
static void Scan(IPAddress ip, int startPort, int endPort)
{
Random rand = new Random((int)DateTime.Now.Ticks);
Console.WriteLine("Begin Scan...");
for (int port = startPort; port < endPort; port++)
{
Socket scanSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
do
{
try
{
scanSocket.Bind(new IPEndPoint(IPAddress.Any, rand.Next(65535)));
break;
}
catch
{
}
} while (true);
try
{
scanSocket.BeginConnect(new IPEndPoint(ip, port), ScanCallBack, new ArrayList() { scanSocket, port});
}
catch
{
continue;
}
}
Console.WriteLine("Port Scan Completed!");
}
static void ScanCallBack(IAsyncResult result)
{
ArrayList arrList = (ArrayList)result.AsyncState;
Socket scanSocket = (Socket)arrList[0];
int port = (int)arrList[1];
if (result.IsCompleted && scanSocket.Connected)
{
Console.WriteLine("port {0,5}tOpen.", port);
}
else
{
}
scanSocket.Close();
}
}
}
网页名称:C#进行端口扫描的源码
网页路径:http://scpingwu.com/article/iegsec.html