使用线程池进行端口扫描 [英] Port scanning using threadpool

查看:246
本文介绍了使用线程池进行端口扫描的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试运行一个小型应用程序,该应用程序扫描端口并检查是否使用线程池并在使用线程池的情况下将其打开.控制台窗口将询问一个数字,并扫描从1到X的端口,并显示每个端口是打开还是关闭.我的问题是,当它通过每个端口时,有时会过早停止.它也不止于一个数字,而是相当随机的.例如,我指定200.控制台将滚动浏览每个端口,然后在110处停止.下一次我运行它时,它将在80处停止.

I am trying to run a small app that scans ports and checks to see if they are open using and practicing with threadpools. The console window will ask a number and scans ports from 1 to X and will display each port whether they are open or closed. My problem is that as it goes through each port, it sometimes stops prematurely. It doesn't stop at just one number either, its pretty random. For example it I specify 200. The console will scroll through each port then stops at 110. Next time I run it, it stops at 80.

代码 省略一些事情,假设所有变量都在应有的位置声明.第一部分在Main中.

Code Left out some of the things, assume all variables are declared where they should. First part is in Main.

static void Main(string[] args)
    {
        string portNum;
        int convertedNum;
        Console.WriteLine("Scanning ports 1-X");
        portNum = Console.ReadLine();
        convertedNum = Convert.ToInt32(portNum);
        try
        {
            for (int i = 1; i <= convertedNum; i++)
            {
                ThreadPool.QueueUserWorkItem(scanPort, i);
                Thread.Sleep(100);

            }
        }
        catch (Exception e)
        {
           Console.WriteLine("exception " + e);
        }
    }

    static void scanPort(object o)
    {
        TcpClient scanner = new TcpClient();
        try
        {
            scanner.Connect("127.0.0.1",(int)o);
            Console.WriteLine("Port {0} open", o);
        }
        catch
        {
            Console.WriteLine("Port {0} closed",o);
        }
    }

}

推荐答案

如果这是完整的代码,则该错误很可能是由于您陷入了main()的结尾而没有等待所有线程池线程而导致的完成.一旦您的主线程退出main()后退出,ThreadPool线程将全部中止. 尝试删除Thread.Sleep(100)(这不是必需的,这是错误的方法,您永远不知道要睡多长时间,并且部分地破坏了使用ThreadPool的目的),您可能会甚至不检查单个端口!

If this is the entire code, then the error is probably caused by you just falling through to the end of main() without waiting for all your thread pool threads to finish. The ThreadPool threads are all aborted once your main thread exits after falling through main(). Try removing the Thread.Sleep(100) (it is not needed, this is the wrong way, you'd never know for how long to sleep for and it partially defeats the purpose of using a ThreadPool in the first place) and you will probably not even check a single port!

相反,您可以让每个辅助线程都设置一个事件,并在main中使用WaitAll来完成所有事件.请参阅 http://msdn.microsoft.com/en-us/library/3dasc8as .aspx 为例.

Instead you could have each of your worker threads set an event and use WaitAll in main for all events to finish. See http://msdn.microsoft.com/en-us/library/3dasc8as.aspx for an example.

修改: 仔细考虑一下,上面链接中引用的解决方案可能对您来说也不理想(它可能涉及必须分配65000个事件的数组,这太过分了).在.net 4中,您可以像这样使用CountdownEvent:

Thinking this through, the solution referenced at the link above is probably less than ideal for you as well (it might involve having to allocate an array of 65000 events, this would be excessive). In .net 4 you could use a CountdownEvent like this:

对不起,我要运行,但是请检查此示例 http ://msdn.microsoft.com/zh-cn/library/system.threading.countdownevent.aspx ,并在遇到其他问题时通知我们,我相信有人可以并且会详细说明或提出更好的解决方案和更适合.net3的解决方案

Sorry, I gotta run, but check this example http://msdn.microsoft.com/en-us/library/system.threading.countdownevent.aspx and let us know when you have further questions, I'm sure someone can and will elaborate or suggest a better solution and a solution more suitable for .net3

这篇关于使用线程池进行端口扫描的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆