码迷,mamicode.com
首页 > Windows程序 > 详细

c#并行扫描端口控制台程序

时间:2016-05-06 09:25:48      阅读:292      评论:0      收藏:0      [点我收藏+]

标签:

  static void Main(string[] args)
        {
            Console.WriteLine("请输入ip");
            string ip = Console.ReadLine();
            Parallel.For(1, 65535, i => scan(ip, i, 200));
            Console.WriteLine("扫描完成");


        }

        public static void scan(string ip, int port, int timeout)
        {
            TcpClient tc = new TcpClient();
            tc.ReceiveTimeout = timeout;
            try
            {
                tc.Connect(ip, port);
                if (tc.Connected)
                {
                    Console.WriteLine("Port {0} is Open", port.ToString().PadRight(6));
                    Console.WriteLine("连接成功!!!");
                }
            }
            catch
            {
                //Console.WriteLine("Port {0} is Closed", port.ToString().PadRight(6));
            }
            finally
            {
                tc.Close();
                tc = null;

            }
        }

 

c#并行扫描端口控制台程序

标签:

原文地址:http://www.cnblogs.com/gaocong/p/5464391.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!