C#实现一个简单的多线程IP或端口扫描器

using System;
using System.Collections.Generic;

using System.Text;
using System.Net.Sockets;
using System.Net;

using System.Threading;

namespace csPortScanner
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("正在为您寻找IP地址,请稍等");
            //simapleIPScaner();
            for (int i = 2; i < 254; i++)
            {
                ParameterizedThreadStart ps = new ParameterizedThreadStart(Connector);
                Thread scanThead = new Thread(ps);
               // System.Threading.Thread scanThead =
               //new System.Threading.Thread(
               //new System.Threading.ParameterizedThreadStart(Connector));

                scanThead.Start(i);
            }
            Console.ReadKey();

        }
        
        static void Connector(object i)
        {
            string ip = ("172.21.2."+i).ToString();
            IPAddress ipAddress = IPAddress.Parse(ip);
            IPEndPoint ipport = new IPEndPoint(ipAddress, 3128);
            Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            try
            {
                // s.BeginConnect(ipport, new AsyncCallback(EndConnect), s); 
                s.Connect(ipport);
                if (s.Connected == true)
                    Console.WriteLine("找到了,IP为" + ip);

            }
            catch (Exception e)
            {

                //Console.WriteLine(e.Message);
            }

        }
        
        /// <summary>
        /// 最简单的一个IP扫描器
        /// </summary>
        public static  void simapleIPScaner()
        {
            Console.WriteLine("正在为您查找IP");
            Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IPEndPoint ipport = null;

            for (int i = 2; i < 254; i++)
            {



                IPAddress ip = IPAddress.Parse("172.21.2." + i);
                ipport = new IPEndPoint(ip, 3128);
                try
                {
                    // s.BeginConnect(ipport, new AsyncCallback(EndConnect), s); 
                    //s.Connect(ipport);
                    //if (s.Connected == true)
                    

                        Console.WriteLine("找到了,IP为 172.21.2." + i);

                }
                catch (Exception e)
                {

                }
            }
        }

        /*
        private void MainScan()
        {
            if (!AdjustPortAndIP(ip, ref sport, ref eport))
            {
                if (OnPortError != null)
                    OnPortError(null, null);
                return;
            }
            if (maxThreadCount > DEFAULT_THREAD_COUNT)
            {
                maxThreadCount = DEFAULT_THREAD_COUNT;
            }
            realThreadCount = 0;
            
            ScanThread st = new ScanThread();
            st.ScanIP = this.scanIP;
            st.OnPortConnect += new ScanHandler(st_OnPortConnect);
            st.OnPortThreadFinish += new EventHandler(st_OnPortThreadFinish);
            for (int i = sport; i <= eport; i++, realThreadCount++)
            {
                while (realThreadCount - st.FinishThreads > maxThreadCount)
                {
                    Thread.Sleep(10);
                }
                Thread.Sleep(10);
                ParameterizedThreadStart ps = new ParameterizedThreadStart(st.ThreadFunc);
                Thread start = new Thread(ps);
                start.Start(i);
            }
            while (st.FinishThreads != realThreadCount) Thread.Sleep(1000);//等到所有线程全退出为止 
            //Thread.Sleep(10000); 
            if (OnScanFinish != null) //通知客户端扫描已经完成 
                OnScanFinish(null, null);
        }
        */




        /*
        public void ThreadFunc(object o)
        {
            int port = (int)o;
            ScanSocket s = new ScanSocket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            s.Port = port;
            IPEndPoint ipport = new IPEndPoint(scanIP, port);
            try
            {
                // s.BeginConnect(ipport, new AsyncCallback(EndConnect), s); 
                s.Connect(ipport);
                if (s.Connected == true)
                    OnPortConnect(null, new ScanEventArg(s.Port));
            }
            catch (Exception e)
            {

            }
            finally
            {
                lock (this)
                {
                    finishThreads++;
                }
                if (OnPortThreadFinish != null)
                    OnPortThreadFinish(null, null);
            }
        }
         */
    }
}

 

posted on 2013-11-14 01:06  baijunzhen  阅读(1206)  评论(0)    收藏  举报

导航