温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

C#进行端口扫描的源码

发布时间:2020-06-21 00:44:04 来源:网络 阅读:364 作者:Waddell 栏目:编程语言

下面代码内容是关于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();
        }
    }
}
向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI